2.4.4 • Published 10 months ago

talt v2.4.4

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

Talt

github actions npm version GitHub license

Template functions to generate TypeScript AST, inspired from @babel/template .

Install

$ npm i talt typescript

Usage

import ts from "typescript";
import { template } from "talt";

const typeNode = template.typeNode("{ readonly hoge: string }")();

// You can use `template` as tag function.
const typeNodeUsingTagFn = template.typeNode`
  {
    readonly hoge: string;
  }
`();

// The following returns ts.BinaryExpression
const binaryExpression = template.expression<ts.BinaryExpression>("60 * 1000")();

// You can use identifier placeholder.
const compiledFn = template.expression`
  60 * SOME_PLACEHOLDER_KEY
`;
const generatedAst = compiledFn({
  SOME_PLACEHOLDER_KEY: binaryExpression,
}); // returns expression node, `60 * 60 * 1000`

const generetedOtherNode = compiledFn({
  SOME_PLACEHOLDER_KEY: ts.factory.createNumericLiteral("200"),
}); // returns expression node, `60 * 200`

// You can use any function which returns ts.Node instead of identifier placeholder.
const altCompiledFn = template.expression`
  60 * ${() => binaryExpression}
`; // returns expression node, `60 * 60 * 1000`

API

template has the following tag functions. Each tag function compiles and provides corresponding type AST.

  • template.typeNode
  • template.expression
  • template.statement
  • template.sourceFile
  • template.jsxAttribute

License

MIT

2.4.4

10 months ago

2.4.3

1 year ago

2.4.1

1 year ago

2.4.0

1 year ago

2.4.2

1 year ago

2.3.0

2 years ago

2.3.1

2 years ago

2.2.3

2 years ago

2.2.1

3 years ago

2.2.2

3 years ago

2.2.0

3 years ago

2.1.0

3 years ago

2.0.0

3 years ago

1.0.0

3 years ago

1.0.0-alpha.0

3 years ago