Avro TypeScript Generator

1.3.0 · active · verified Sun Apr 19

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`.

Common errors

Warnings

Install

Imports

Quickstart

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`.

import { avroToTypeScript, RecordType } from 'avro-typescript';
import * as fs from 'fs';
import * as path from 'path';

// Define a sample Avro schema (typically loaded from a .avsc file)
const avroSchema: RecordType = {
  type: 'record',
  name: 'UserProfile',
  namespace: 'com.example.user',
  fields: [
    { name: 'id', type: 'string' },
    { name: 'username', type: 'string' },
    {
      name: 'email',
      type: ['null', 'string'],
      default: null,
    },
    {
      name: 'createdAt',
      type: { type: 'long', logicalType: 'timestamp-millis' },
    },
    {
      name: 'status',
      type: {
        type: 'enum',
        name: 'UserStatus',
        symbols: ['ACTIVE', 'INACTIVE', 'PENDING'],
      },
      default: 'PENDING',
    },
    {
        name: 'tags',
        type: { type: 'array', items: 'string' },
        default: [],
    },
  ],
};

// Options for generating TypeScript, including logical type overrides
const generationOptions = {
  logicalTypes: {
    'timestamp-millis': 'Date',
  },
};

// Generate TypeScript code
const typescriptCode = avroToTypeScript(avroSchema, generationOptions);

console.log('Generated TypeScript:\n');
console.log(typescriptCode);

// Example of writing to a file (optional)
const outputPath = path.join(process.cwd(), 'generated-types.d.ts');
fs.writeFileSync(outputPath, typescriptCode);
console.log(`\nTypeScript types written to ${outputPath}`);

/*
  Expected output (simplified):

  Generated TypeScript:

  export namespace com.example.user {
    export interface UserProfile {
      id: string;
      username: string;
      email: string | null;
      createdAt: Date;
      status: UserStatus;
      tags: string[];
    }

    export type UserStatus = 'ACTIVE' | 'INACTIVE' | 'PENDING';
  }

  TypeScript types written to .../generated-types.d.ts
*/

view raw JSON →