Compose macro in Typescript, expand back into Typescript
tsc-macro evaluates each [name].macro.ts
file and save the result to corresponding [name].ts
Source file: fruit.macro.ts
import { genEnum } from 'tsc-macro'
genEnum('fruit', ['apple', 'orange'])
Generated file: fruit.ts
export enum fruit {
apple,
orange,
}
A more flexible example: color.macro.ts
import { genArray, genUnionType } from 'tsc-macro'
let colors = ['red', 'green', 'blue']
;`
${genUnionType('Color', colors)}
${genArray('values', colors)}
export const colors: Color[] = ${genArray(colors)}
`.trim()
Generated into color.ts
export type Color =
| 'red'
| 'green'
| 'blue'
export const values = [
'red',
'green',
'blue',
]
export const colors: Color[] = [
'red',
'green',
'blue',
]
npm i -D tsc-macro
## recursively in the current directory
npx tsc-macro
## recursively in given directory
npx tsc-macro src/models
This project is licensed with BSD-2-Clause
This is free, libre, and open-source software. It comes down to four essential freedoms [ref]: