{"id":15241,"library":"talt","title":"TypeScript AST Template Generator","description":"Talt is a utility library that provides template functions for generating TypeScript Abstract Syntax Tree (AST) nodes programmatically. Inspired by Babel's `@babel/template`, it allows developers to define AST structures using familiar TypeScript syntax within template strings, which are then parsed and transformed into `ts.Node` objects. This significantly simplifies the process of creating and manipulating TypeScript ASTs compared to using the `typescript.factory` API directly. The current stable version is 2.4.5, with releases occurring as needed for bug fixes and feature enhancements, as seen in recent patch versions. Its primary differentiator is the template-based approach, which enhances readability and reduces boilerplate when working with complex AST constructions, especially useful in code transformation, linting, or transpilation tools. It relies on `typescript` as a peer dependency for its AST types and parsing capabilities.","status":"active","version":"2.4.5","language":"javascript","source_language":"en","source_url":"https://github.com/Quramy/talt","tags":["javascript","TypeScript","AST","typescript"],"install":[{"cmd":"npm install talt","lang":"bash","label":"npm"},{"cmd":"yarn add talt","lang":"bash","label":"yarn"},{"cmd":"pnpm add talt","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for TypeScript AST types and factory functions. It is a peer dependency.","package":"typescript","optional":false}],"imports":[{"note":"The primary utility `template` is a named export. It is an object containing various tag functions.","wrong":"import template from 'talt';","symbol":"template","correct":"import { template } from 'talt';"},{"note":"Specific template functions like `typeNode`, `expression`, `statement`, `sourceFile`, and `jsxAttribute` are properties of the `template` object.","symbol":"template.typeNode","correct":"import { template } from 'talt';\nconst node = template.typeNode`{ readonly prop: string }`();"},{"note":"While not directly from 'talt', the `typescript` module (imported as `ts`) is fundamental for working with Talt, as Talt generates `ts.Node` objects. The default import style for TypeScript's own module is generally preferred.","wrong":"import * as ts from 'typescript';","symbol":"ts","correct":"import ts from 'typescript';"}],"quickstart":{"code":"import ts from 'typescript';\nimport { template } from 'talt';\n\n// Create a type node using a template string\nconst interfaceTypeNode = template.typeNode`\n  interface MyInterface {\n    id: string;\n    createdAt: Date;\n  }\n`();\n\n// Create an expression node with a placeholder\nconst mathExpressionTemplate = template.expression`\n  (BASE_VALUE * 2) + ${() => ts.factory.createNumericLiteral('10')}\n`;\n\nconst generatedExpression = mathExpressionTemplate({\n  BASE_VALUE: ts.factory.createNumericLiteral('50')\n});\n\n// You can then print the generated AST node to verify\nconst printer = ts.createPrinter({\n  newLine: ts.NewLineKind.LineFeed\n});\nconst resultFile = ts.createSourceFile('temp.ts', '', ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);\nconst printedNode = printer.printNode(ts.EmitHint.Unspecified, generatedExpression, resultFile);\n\nconsole.log('Generated Interface Type Node:\\n', printer.printNode(ts.EmitHint.Unspecified, interfaceTypeNode, resultFile));\nconsole.log('Generated Expression Node:\\n', printedNode);","lang":"typescript","description":"This example demonstrates how to use `talt` to generate complex TypeScript AST nodes, including type declarations and expressions with placeholders, and then print them."},"warnings":[{"fix":"Run `npm install typescript` or `yarn add typescript` in your project.","message":"Talt is a peer dependency on `typescript`. Ensure `typescript` is installed in your project at a compatible version range (`^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0`) to avoid runtime errors.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Double-check placeholder names in template strings and the replacement object. For example, `SOME_KEY` in the template requires `{ SOME_KEY: ... }`.","message":"When using identifier placeholders in templates, ensure the placeholder keys match the property names in the object passed to the compiled template function. Mismatched keys will result in `undefined` nodes or parsing errors.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Refer to the TypeScript compiler API documentation and examples when encountering unexpected AST structures.","message":"While Talt simplifies AST generation, understanding fundamental TypeScript AST concepts and the `typescript` module's `factory` methods is still beneficial for debugging complex templates or handling cases not covered by direct templating.","severity":"gotcha","affected_versions":"*"},{"fix":"No fix needed for existing code. If adopting the new function-based placeholder, update your templates accordingly: `template.expression` `60 * ${() => myNode}` instead of `60 * SOME_PLACEHOLDER_KEY`.","message":"In Talt v2.4.0, a new feature was introduced allowing functions `() => ts.Node` as placeholders. While not strictly 'breaking', older code relying solely on identifier placeholders might need adjustment if adopting this more flexible function-based approach for node replacement.","severity":"breaking","affected_versions":"<2.4.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"npm install typescript","cause":"The `typescript` package is a peer dependency and must be installed separately.","error":"Error: Cannot find module 'typescript'"},{"fix":"Ensure `import ts from 'typescript';` is at the top of your file and `typescript` is installed.","cause":"This usually indicates `ts` was not correctly imported from the `typescript` package, or an attempt was made to use a `ts.factory` method without `ts` being available.","error":"TypeError: Cannot read properties of undefined (reading 'factory')"},{"fix":"Ensure that every identifier placeholder (e.g., `SOME_PLACEHOLDER_KEY`) in your template string has a corresponding key in the object passed to the template function, mapping to a `ts.Node` or a string.","cause":"A placeholder identifier used in the template string was not provided in the replacement object when calling the compiled template function.","error":"Error: Talt: Unknown identifier SOME_PLACEHOLDER_KEY"}],"ecosystem":"npm"}