Tanu.js

0.2.0 · active · verified Sun Apr 19

Tanu.js is a JavaScript/TypeScript library designed to simplify the generation of TypeScript types and interfaces by providing a high-level, declarative abstraction over the complex TypeScript Compiler API. It specifically aims to mitigate the common practice of generating `.d.ts` files using error-prone, untyped template literal strings, promoting greater type safety and readability in generated code. Currently at version 0.2.0, the library is in an early, active development phase, implying potential for rapid evolution and API changes. Its key differentiator is a fluent API for defining TypeScript constructs like interfaces, enums, and types, including a sophisticated mechanism for handling self-referencing and cross-referencing types through lazy evaluation callback functions.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates defining several TypeScript interfaces and an enum, including nested and optional types, and then generating the corresponding TypeScript source code.

import { t } from 'tanu.js';

const User = t.interface('User', {
  id: t.number(),
  email: t.string(),
  name: t.optional({
    first: t.string(),
    last: t.string()
  })
});

const MemberRole = t.enum('MemberRole', [
  'DEFAULT',
  'PRIVILEGED',
  'ADMINISTRATOR'
]);

const Member = t.interface('Member', {
  user: User,
  role: MemberRole
});

const Organization = t.interface('Organization', {
  name: t.comment(t.string(), [
    'The organization name.',
    '@see https://example.com/organization-name'
  ]),
  description: t.optional(t.string()),
  members: t.array(Member)
});

async function generateTypes() {
  const result = await t.generate([User, MemberRole, Member, Organization]);
  console.log(result);
}

generateTypes();

view raw JSON →