{"id":12118,"library":"tanu","title":"Tanu.js","description":"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.","status":"active","version":"0.2.0","language":"javascript","source_language":"en","source_url":null,"tags":["javascript","typescript","compiler api","abstraction","wrapper","tsc","generation"],"install":[{"cmd":"npm install tanu","lang":"bash","label":"npm"},{"cmd":"yarn add tanu","lang":"bash","label":"yarn"},{"cmd":"pnpm add tanu","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Tanu.js is an abstraction layer over the TypeScript Compiler API and therefore requires 'typescript' to be available in the project's dependencies or peer dependencies for full functionality.","package":"typescript","optional":false}],"imports":[{"note":"Tanu.js is published as an ESM-first library. While some bundlers might transpile CommonJS 'require', direct usage is generally discouraged in favor of native ES Modules.","wrong":"const t = require('tanu.js');","symbol":"t","correct":"import { t } from 'tanu.js';"},{"note":"Type definition functions like 'interface', 'enum', 'array', etc., are methods of the main 't' object and are not top-level named exports.","wrong":"import { interface } from 'tanu.js';","symbol":"t.interface","correct":"import { t } from 'tanu.js'; const MyInterface = t.interface('MyInterface', { /* ... */ });"},{"note":"The 'generate' function is a method of the 't' object and is typically used asynchronously to produce the final TypeScript string output.","wrong":"import { generate } from 'tanu.js';","symbol":"t.generate","correct":"import { t } from 'tanu.js'; const result = await t.generate([/* ... */]);"}],"quickstart":{"code":"import { t } from 'tanu.js';\n\nconst User = t.interface('User', {\n  id: t.number(),\n  email: t.string(),\n  name: t.optional({\n    first: t.string(),\n    last: t.string()\n  })\n});\n\nconst MemberRole = t.enum('MemberRole', [\n  'DEFAULT',\n  'PRIVILEGED',\n  'ADMINISTRATOR'\n]);\n\nconst Member = t.interface('Member', {\n  user: User,\n  role: MemberRole\n});\n\nconst Organization = t.interface('Organization', {\n  name: t.comment(t.string(), [\n    'The organization name.',\n    '@see https://example.com/organization-name'\n  ]),\n  description: t.optional(t.string()),\n  members: t.array(Member)\n});\n\nasync function generateTypes() {\n  const result = await t.generate([User, MemberRole, Member, Organization]);\n  console.log(result);\n}\n\ngenerateTypes();","lang":"typescript","description":"Demonstrates defining several TypeScript interfaces and an enum, including nested and optional types, and then generating the corresponding TypeScript source code."},"warnings":[{"fix":"Refer to the latest documentation and changelog for updates, and lock to exact dependency versions for production applications.","message":"As of version 0.2.0, Tanu.js is an early-stage library. Its API surface may undergo significant changes and breaking modifications in future minor or patch releases until it reaches a stable 1.0.0 version.","severity":"gotcha","affected_versions":">=0.2.0"},{"fix":"Wrap your type definition object in an arrow function, for example: `t.interface('MyType', () => ({ selfRef: MyType }))`.","message":"When defining interfaces or types that are self-referencing or mutually recursive (e.g., a 'User' interface that contains an array of 'User' objects), it is crucial to use the callback pattern for `t.interface` or `t.type` to enable lazy evaluation. Failing to do so will result in runtime errors.","severity":"gotcha","affected_versions":">=0.2.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Wrap the type definition object in an arrow function to allow for lazy resolution of the type reference: `const MyType = t.interface('MyType', () => ({ prop: MyType }));`","cause":"Attempting to define a self-referencing or mutually recursive type without utilizing the lazy evaluation callback function for `t.interface` or `t.type`.","error":"ReferenceError: Cannot access 'MyType' before initialization"},{"fix":"Ensure the package is installed: `npm install tanu.js` or `yarn add tanu.js`. Check your `package.json` for correct installation.","cause":"The `tanu.js` package has not been installed or is not correctly resolved by your module bundler/Node.js environment.","error":"Error: Module not found: Can't resolve 'tanu.js' in '...' OR Cannot find module 'tanu.js' from '...'"}],"ecosystem":"npm"}