https://github.com/beenotung/tsc-macro
Compose macro in Typescript, expand back into Typescript
https://github.com/beenotung/tsc-macro
code-generation macros typescript
Last synced: 30 days ago
JSON representation
Compose macro in Typescript, expand back into Typescript
- Host: GitHub
- URL: https://github.com/beenotung/tsc-macro
- Owner: beenotung
- License: bsd-2-clause
- Created: 2019-10-16T06:56:22.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-10-15T10:50:39.000Z (12 months ago)
- Last Synced: 2025-08-03T14:52:12.338Z (2 months ago)
- Topics: code-generation, macros, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/tsc-macro
- Size: 29.3 KB
- Stars: 12
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tsc-macro
Compose macro in Typescript, expand back into Typescript[](https://www.npmjs.com/package/tsc-macro)
## How it works
tsc-macro evaluates each `[name].macro.ts` file and save the result to corresponding `[name].ts`
## Example
Source file: `fruit.macro.ts`
```typescript
import { genEnum } from 'tsc-macro'genEnum('fruit', ['apple', 'orange'])
```
Generated file: `fruit.ts`
```typescript
export enum fruit {
apple,
orange,
}
```A more flexible example: `color.macro.ts`
```typescript
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`
```typescript
export type Color =
| 'red'
| 'green'
| 'blue'export const values = [
'red',
'green',
'blue',
]export const colors: Color[] = [
'red',
'green',
'blue',
]
```[More Examples](./examples)
## Installation
```bash
npm i -D tsc-macro
```## Transpile
```bash
## recursively in the current directory
npx tsc-macro## recursively in given directory
npx tsc-macro src/models
```## License
This project is licensed with [BSD-2-Clause](./LICENSE)
This is free, libre, and open-source software. It comes down to four essential freedoms [[ref]](https://seirdy.one/2021/01/27/whatsapp-and-the-domestication-of-users.html#fnref:2):
- The freedom to run the program as you wish, for any purpose
- The freedom to study how the program works, and change it so it does your computing as you wish
- The freedom to redistribute copies so you can help others
- The freedom to distribute copies of your modified versions to others