Registry / serialization / talt
library2.4.5jsnpmunverified

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 talt
INSTALL
IMPORT
SIG · TALT
T
talt
serializationjavascriptv2.4.5
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

template
import { template } from 'talt';
import template from 'talt';
The primary utility `template` is a named export. It is an object containing various tag functions.
template.typeNode
import { template } from 'talt'; const node = template.typeNode`{ readonly prop: string }`();
Specific template functions like `typeNode`, `expression`, `statement`, `sourceFile`, and `jsxAttribute` are properties of the `template` object.
ts
import ts from 'typescript';
import * as ts from 'typescript';
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.

This example demonstrates how to use `talt` to generate complex TypeScript AST nodes, including type declarations and expressions with placeholders, and then print them.

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);
Debug
Known issues
gotchaTalt 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.
fix
Run `npm install typescript` or `yarn add typescript` in your project.
affects: >=2.0.0
gotchaWhen 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.
fix
Double-check placeholder names in template strings and the replacement object. For example, `SOME_KEY` in the template requires `{ SOME_KEY: ... }`.
affects: >=2.0.0
gotchaWhile 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.
fix
Refer to the TypeScript compiler API documentation and examples when encountering unexpected AST structures.
affects: *
breakingIn 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.
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`.
affects: <2.4.0
Errors
Common errors & fixes
Error: Cannot find module 'typescript'
The `typescript` package is a peer dependency and must be installed separately.
fix
npm install typescript
TypeError: Cannot read properties of undefined (reading 'factory')
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.
fix
Ensure `import ts from 'typescript';` is at the top of your file and `typescript` is installed.
Error: Talt: Unknown identifier SOME_PLACEHOLDER_KEY
A placeholder identifier used in the template string was not provided in the replacement object when calling the compiled template function.
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.
Upgrade
Version history
2.4.5latest on npm
Audit
Dependencies
typescriptrequiredRequired for TypeScript AST types and factory functions. It is a peer dependency.
Agent activity
6 hits · last 30 days
node
6
Resources
talt — npm install talt · libregistry