DevTools Logo

Avro Schema to TypeScript

Avro Schema to TypeScript

Convert Apache Avro 1.11 schemas into TypeScript types and JSON Schema, with every lossy mapping flagged.

Runs in your browser
Try an example

Avro schema

47 lines · 713 chars

TypeScript

11 lines · 236 chars
Avro long is 64-bit. TypeScript number is exact only up to 2^53 - 1; larger values lose precision. Use the bigint option if your values can exceed that.
timestamp-*, local-timestamp-* and date logical types are typed as number (epoch milliseconds, microseconds, or days). Turn on the Date option to type them as Date.
JSON Schema uses anyOf for unions and describes plain JSON. Avro's own JSON encoding wraps non-null union values as {"type": value}, which this schema does not accept.

Details

Paste an Avro schema JSON document and generate deterministic TypeScript interfaces or JSON Schema. Primitive mappings, nullable unions, defaults, enums, fixed values, and logical type notes are supported locally.

Examples

Nullable field plus enum

Input
compileAvroSchema({ type: "record", name: "User", fields: [{ name: "nickname", type: ["null", "string"] }, { name: "status", type: { type: "enum", name: "Status", symbols: ["NEW", "OLD"] } }] })
Output
{ typescript: "nickname: string | undefined; ... export type Status = \"NEW\" | \"OLD\";" }

Null unions sort undefined last and enums become literal type aliases.

Logical type annotation

Input
compileAvroSchema({ type: "record", name: "E", fields: [{ name: "when", type: { type: "long", logicalType: "timestamp-millis" } }] })
Output
{ typescript contains "when: number; // logicalType: timestamp-millis" }

Wrapped primitive logical types surface as field comments plus JSON Schema descriptions.

About this tool

Avro Schema to TypeScript compiles schemas with compileAvroSchema(schema) and compileAvroJson(raw) (aliases avroSchemaToTypeScript/avroToTypeScript, avroSchemaToJsonSchema/avroToJsonSchema): primitives map null → undefined, boolean → boolean, int/long/float/double → number, bytes → Uint8Array, and string → string, while unions join deduplicated branches with undefined sorted last, so ["null", "string"] and ["string", "null"] both emit string | undefined.

Named types emit declarations: record becomes an interface with doc/default comments per field, enum becomes a string-literal union type alias, fixed becomes Uint8Array with a size comment, and logicalType appends a /* logicalType: x */ note (surfaced as a field comment for wrapped primitives). The root declaration leads and remaining declarations sort alphabetically, making repeated compiles byte-identical.

JSON Schema output targets draft 2020-12 with sorted $defs: bytes map to base64 strings, numeric Avro types to number, records to object properties with every field required (defaults preserved), enums to string enums, and logicalType to a description. Invalid JSON throws Avro schema is not valid JSON, empty unions and missing fields/items/values/symbols/size throw descriptive errors.

How to use this tool

  1. Paste the schema

    Provide an Avro JSON object, type string, or union array — or raw JSON text to compileAvroJson.

  2. Read the types

    Copy the root-first TypeScript: interfaces for records, literal unions for enums, Uint8Array for bytes/fixed.

  3. Validate with JSON Schema

    Use the $defs output for runtime validation alongside the static types.

  4. Check errors

    Malformed JSON, empty unions, and records without fields report explicit messages.

Use cases

Typing event pipelines

Generate client interfaces from registry Avro schemas for Kafka or Pulsar topics.

Bridging static and runtime checks

Ship TypeScript types for editors plus JSON Schema for payload validation.

Documenting nullable contracts

Make optional semantics explicit with T | undefined instead of silent nulls.

Common mistakes

Mistake:Expecting null to stay null in TypeScript.

Fix:Avro null maps to undefined by design — nullable unions read string | undefined.

Mistake:Assuming declaration order follows field order.

Fix:Non-root declarations sort alphabetically, so Apple precedes Zebra regardless of field position.

Frequently asked questions

What does this tool do?

It compiles Avro schemas with compileAvroSchema (or raw JSON with compileAvroJson) into TypeScript declarations plus draft 2020-12 JSON Schema with sorted $defs — records to interfaces, enums to literal unions, bytes/fixed to Uint8Array.

How are nullables, logical types, and ordering handled?

Null maps to undefined with unions emitting T | undefined (undefined last regardless of input order); logicalType appends a note and field comment plus a JSON Schema description; the root leads and remaining declarations sort alphabetically with byte-identical repeats.

Is my schema uploaded or stored?

No. Parsing, type mapping, and schema emission run entirely in your browser.

Why did compilation throw, or null become undefined?

Invalid JSON throws Avro schema is not valid JSON while empty unions and records without fields throw descriptive errors; null → undefined is by design, so nullable fields read string | undefined.

References & standards