0.3.0 • Published 4 years ago
ts-annotate v0.3.0
ts-annotate
Automatically adds TypeScript declarations based on actual types passed at runtime, as determined by V8.
Before
function add(a, b) {
return a + b;
}
add(1, 2);After
function add(a: number, b: number): number {
return a + b;
}
add(1, 2);Instructions
# 1. Generate type data by running the code
npx ts-annotate run ./src/index.js
# 2. Add type declaration to source code
npx ts-annotate apply ./src/**/*.js
# 3. Rename .js to .ts
# 4. Cleanup/improve typesSupported types
- Primitives -
string,number,boolean, etc. - Class instances(includes arrays, objects, promises, etc.)
- Functions(i.e. callbacks)
Only simple types are inferred - objects are Record<string, any>, arrays
any[], and functions (...args: any[]) => any.
Commands
run- Same as runningnodebut saves type data tots-annotate-map.jsonapply- Adds type declarations to the specified files using data ints-annotate-map.json
Passing flags to Node
node --flag ./node_modules/.bin/ts-annotate ...