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.
npm install taltVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to use `talt` to generate complex TypeScript AST nodes, including type declarations and expressions with placeholders, and then print them.
Run `npm install typescript` or `yarn add typescript` in your project.
Double-check placeholder names in template strings and the replacement object. For example, `SOME_KEY` in the template requires `{ SOME_KEY: ... }`.Refer to the TypeScript compiler API documentation and examples when encountering unexpected AST structures.
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`.npm install typescript
Ensure `import ts from 'typescript';` is at the top of your file and `typescript` is installed.
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.