# ts-annotate

> Auto-annotate TypeScript based on runtime profiling

Latest version **0.3.0** (published 2022-01-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install ts-annotate
pnpm add ts-annotate
yarn add ts-annotate
bun add ts-annotate
```

Provides the command `ts-annotate`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2022-01-31 |
| First published | 2022-01-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=14 |
| Dependencies | 4 |
| Unpacked size | 8.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Johan Holmerin |
| Maintainers | johanholmerin |

## Links

- npm: https://www.npmjs.com/package/ts-annotate
- Repository: https://github.com/johanholmerin/ts-annotate
- Homepage: https://github.com/johanholmerin/ts-annotate#readme
- Issues: https://github.com/johanholmerin/ts-annotate/issues
- npm.io page: https://npm.io/package/ts-annotate

## Dependencies (4)

- [commander](https://npm.io/package/commander.md) ^9.0.0
- [magic-string](https://npm.io/package/magic-string.md) ^0.25.7
- [@babel/parser](https://npm.io/package/@babel/parser.md) ^7.16.12
- [@babel/traverse](https://npm.io/package/@babel/traverse.md) ^7.16.10

## Recent versions

- 0.3.0 (latest) — 2022-01-31
- 0.2.1 — 2022-01-31
- 0.2.0 — 2022-01-30
- 0.1.1 — 2022-01-30
- 0.1.0 — 2022-01-30

## README

# ts-annotate

Automatically adds TypeScript declarations based on actual types passed at runtime, as determined by [V8][v8-profiling].

#### Before

```javascript
function add(a, b) {
  return a + b;
}

add(1, 2);
```

#### After

```javascript
function add(a: number, b: number): number {
  return a + b;
}

add(1, 2);
```

## Instructions

```sh
# 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

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

## Prior work

- https://github.com/fhinkel/type-profile

[v8-profiling]: https://docs.google.com/document/d/1JY7pUCAk8gegyi6UkIdln6j_AeJqQucZg92advaMJY4/edit

---
_Source: https://npm.io/package/ts-annotate · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
