Adds TypeScript type annotations using static analysis.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
.vscode
bin
docs
src
test
.gitattributes
.gitignore
.markdownlint.json
.npmignore
.prettierignore
.prettierrc.json
README.md
jest.config.js
package.json
tsconfig.json
tslint.json
yarn.lock

README.md

TypeStat

Circle CI NPM version Join the chat at https://gitter.im/TypeStat/community Code Style: Prettier

Adds TypeScript type annotations using static analysis.

💀 Danger: new and experimental; use at your own risk! 💀

Why?

Type safety with TypeScript, especially when adding --noImplicitAny or --strictNullChecks, is wonderful but can be difficult to add to large pre-existing projects. Converting from JavaScript to TypeScript or improving type annotations through hundreds or thousands of legacy files is tedious and time-consuming.

This package automagically adds those missing type annotations for you with configurable comment markers. It can add...

TypeScript types on files freshly converted from JavaScript to TypeScript!

Fixes for --noImplicitAny across all your files with one command!

Missing nulls and undefineds to get you started with --strictNullChecks!

Doing so can allow you to enable these stricter compiler flags for all code without changing the runtime of existing code. Hooray! 💪

👉 Protip: also take a look at TypeWiz! 👈

Installation

npm i -g typestat

TypeStat can be installed globally or as a project dependency via npm/yarn.

CLI

typestat

This will use Cosmiconfig to search for a package.json "typestat" property or configuration file such to read settings from.

You can run on only a subset of files by passing globs to the command:

typestat src/demo/*.ts src/utils/**/*.ts

For instructions on the types of changes you can run with Typestat, see:

  1. Usage.md for recommended steps to get started
  2. Fixes.md for the type of fixes TypeStat will generate mutations for
  3. Types.md for configuring how to rename types or add comments in mutations
  4. Nodes.md for details on which parts of source files TypeStat will change and how
  5. Filters.md for using tsquery to ignore sections of source files
  6. Custom Mutators.md for including or creating custom mutators

Basic Flags

-c/--config

Path to a TypeStat configuration file, if you'd like a custom path.

typestat --config typestat.custom.json

include

Glob patterns of file names to include. You can specify these as arguments to the CLI and/or through your TypeStat configuration file. CLI includes will replace any configuration file includes.

typestat ./src/**/*.ts
{
    "include": [
        "./src/**/*.ts"
    ]
}

-p/--project

Path to a TypeScript project file, if you'd like a path other than ./tsconfig.json.

typestat --project tsconfig.strict.json
{
    "project": "tsconfig.strict.json"
}

More advanced flags can be provided for:

  • Files
    • --fileAbove/files.above
    • --fileBelow/files.below
    • --fileConvertFileExtensions/files.convertFileExtensions
  • Filters
    • --filter/filter
  • Fixes:
    • --fixIncompleteTypes/fixes.incompleteTypes
    • --fixMissingProperties/fixes.missingProperties
    • --fixNoImplicitAny/fixes.noImplicitAny
    • --fixStrictNonNullAssertions/fixes.strictNonNullAssertions
  • Types:
    • --typeAlias/types.aliases
    • --typeMatching/types.matching
    • --typeOnlyPrimitives/types.onlyPrimitives
    • --typeStrictNullChecks/types.strictNullChecks

Node

You can also run TypeStat via its JavaScript API:

import { typeStat } from "typestat";

typeStat()
    .then(result => {
        if (result.succeeded) {
            console.log(`Successfully ran TypeStat.`);
        } else {
            console.error(`Failed running TypeStat: ${result.error}`);
        }
    });

typeStat can optionally take in an object with most CLI/configuration settings. See TypeStatArgv:

await typeStat({
    config: "./typestat.custom.json",
});
await typeStat({
    config: "./typestat.custom.json",
    fixIncompleteTypes: true,
    fixNoImplicitAny: true,
    fixStrictNonNullAssertions: true,
    mutators: ["my-custom-mutator"],
    project: "./tsconfig.custom.json",
});

Development

See Development.md.