0.3.0 • Published 2 years ago

ts-annotate v0.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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 types

Supported 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 running node but saves type data to ts-annotate-map.json
  • apply - Adds type declarations to the specified files using data in ts-annotate-map.json

Passing flags to Node

node --flag ./node_modules/.bin/ts-annotate ...

Prior work