tscm
v0.1.3
Published
TypeScript Compiler Macros
Downloads
1
Readme
tscm
Function-like macros for TypeScript, inspired by Rust
- Fully type-checked
- IDE support - correct messages, types and diagnostics while editing
- easy to use, easy to write
- Generates correct source-maps
- Compatible Babel plugin
For a collection of example macros, like GraphQL or SQL, see the tscm-examples repository.
Installation
Bring your own TypeScript, version 4.0 or higher
# Yarn
yarn add -D tscm typescript
# npm
npm install -D tscm typescript
Example
Usage
// macros are imported like normal
import { macro } from './macros';
// Two non-null-assertion operators are used as indicator for macro calls
const val = macro!!('literal', identifier);
Macro definition
Macros are normal functions that get the CallExpression Node as parameter and return a new Node. They can be local files or npm packages. For more information see the Writing macros guide.
const t = require('@babel/types');
/**
* @type {import('tscm/macro').Macro}
*/
module.exports.macro = function ({ node }) {
// Returns that arguments as an array
return t.arrayExpression(node.arguments);
};
Compiling
To compile, use tscm instead of tsc. If you are using a bundler, see Bundler setup.
npx tscm
Editor config
To get the correct types in your editor, make sure to point it to tscm
.
coc.nvim
// .vim/coc-settings.json
{
"tsserver.ignoreLocalTsserver": true,
"tsserver.tsdk": "./node_modules/tscm/typescript/lib"
}
VS Code
Make sure to select Use Workspace Version
under TypeScript: Select TypeScript version
// .vscode/settings.json
{
"typescript.tsdk": "./node_modules/tscm/typescript/lib"
}