TypeScript AST Template Generator
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.
Common errors
-
Error: Cannot find module 'typescript'
cause The `typescript` package is a peer dependency and must be installed separately.fixnpm install typescript -
TypeError: Cannot read properties of undefined (reading 'factory')
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.fixEnsure `import ts from 'typescript';` is at the top of your file and `typescript` is installed. -
Error: Talt: Unknown identifier SOME_PLACEHOLDER_KEY
cause A placeholder identifier used in the template string was not provided in the replacement object when calling the compiled template function.fixEnsure 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.
Warnings
- gotcha 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.
- gotcha 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.
- gotcha 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.
- breaking 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.
Install
-
npm install talt -
yarn add talt -
pnpm add talt
Imports
- template
import template from 'talt';
import { template } from 'talt'; - template.typeNode
import { template } from 'talt'; const node = template.typeNode`{ readonly prop: string }`(); - ts
import * as ts from 'typescript';
import ts from 'typescript';
Quickstart
import ts from 'typescript';
import { template } from 'talt';
// Create a type node using a template string
const interfaceTypeNode = template.typeNode`
interface MyInterface {
id: string;
createdAt: Date;
}
`();
// Create an expression node with a placeholder
const mathExpressionTemplate = template.expression`
(BASE_VALUE * 2) + ${() => ts.factory.createNumericLiteral('10')}
`;
const generatedExpression = mathExpressionTemplate({
BASE_VALUE: ts.factory.createNumericLiteral('50')
});
// You can then print the generated AST node to verify
const printer = ts.createPrinter({
newLine: ts.NewLineKind.LineFeed
});
const resultFile = ts.createSourceFile('temp.ts', '', ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
const printedNode = printer.printNode(ts.EmitHint.Unspecified, generatedExpression, resultFile);
console.log('Generated Interface Type Node:\n', printer.printNode(ts.EmitHint.Unspecified, interfaceTypeNode, resultFile));
console.log('Generated Expression Node:\n', printedNode);