avro-typescript is a dedicated library for generating TypeScript interfaces from Apache Avro schemas. It takes an Avro schema, typically provided as a JavaScript object parsed from JSON, and outputs the corresponding TypeScript code as a string. The library is currently at version 1.3.0 and appears to be actively maintained, with recent updates addressing issues like top-level enum support. It supports most standard Avro features, including enumerated types, maps, named records, unions, and primitives, along with mandatory and optional fields. A key differentiator is its ability to override logical Avro types (e.g., converting an Avro `int` with a `date` logical type to a TypeScript `Date` object) by passing a mapping in the options. This tool operates effectively in both Node.js and browser environments, focusing solely on type generation rather than schema parsing or serialization/deserialization, which often relies on companion libraries like `avsc`.
npm install avro-typescriptVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `avro-typescript` to generate TypeScript interfaces from an Avro schema, including handling optional fields, enums, arrays, and logical type overrides to map Avro `timestamp-millis` to TypeScript `Date`.
Manually manage output file structure or use an external tool to wrap generated types in TypeScript namespaces if strict namespace isolation is required. Be aware of potential name collisions if schemas from different Avro namespaces define types with identical names.
Always pass a `logicalTypes` map in the options object to `avroToTypeScript` to explicitly define the desired TypeScript type for each logical type in your schema, e.g., `{ 'timestamp-millis': 'Date', 'date': 'string' }`.Review generated types for fields with Avro defaults and manually mark them as optional (e.g., `field?: Type;`) in your codebase if strict type-checking of optionality based on Avro defaults is critical for your application logic.
Ensure that the generated TypeScript types are directly accessible without a namespace prefix, or if using a mix of generation tools, adjust your imports or manually wrap `avro-typescript` generated types into the expected namespace. Consider using unique type names across different Avro namespaces if direct namespace support is not available from your chosen generator.
Validate your Avro schema independently using an Avro schema parser or linter before feeding it to `avro-typescript`. Ensure all referenced named types (records, enums, fixed) are correctly defined within the schema or imported if they reside in separate files (though `avro-typescript` expects a single, resolved schema object).
No dependency data recorded yet.