Détail du package

loose-ts-check

Gelio58.2kMIT2.0.0

Run TS type-check and ignore certain errors in some files

typescript, type-check, loose, config

readme

Loose TS check

Downloads badge Version badge License badge GitHub stars badge CI

The loose-ts-check utility helps ignore particular types of TS error in specified files.

This is useful when migrating to a stricter tsconfig.json configuration incrementally, where only some existing files are allowed to have TS errors.

Features

  • ignores specific TS errors from specific files
  • detects files that no longer have to be loosely type-checked
  • auto-updates the loosely type-checked list of files when a file no longer has errors
  • auto-updates the ignored error codes when there are no errors of that type
  • loosely type-checked files can be specified using globs

Why not exclude in tsconfig.json

The exclude option in tsconfig.json only tells tsc to not start type-checking from those files. If some already type-checked file imports a file listed in the exclude, it will still be type-checked.

Thus, it does not suit this use case.

Why not follow the idea from the VSCode project

The vscode team have already encountered a similar problem and solved it in another way. They created a new tsconfig.json that only included some files.

While this works great with existing files, it does not automatically enforce a stricter config for new files in the project.

Installation

Install the utility with:

npm install loose-ts-check --save-dev

Usage

Pipe the result of tsc to loose-ts-check to run the utility:

tsc --noEmit -p tsconfig.strict.json | npx loose-ts-check

To initialize the list of ignored errors and loosely type-checked files based on the current errors, run:

tsc --noEmit -p tsconfig.strict.json | npx loose-ts-check --init

To automatically update the list of loosely type-checked files, run:

tsc --noEmit -p tsconfig.strict.json | npx loose-ts-check --auto-update

Options

Display the list of options by running:

npx loose-ts-check --help

Recipes

Reusing the same tsc output

To avoid running tsc again and again when testing, save the output to a file:

tsc > errors.log;

And then, use the tool by redirecting the input:

npx loose-ts-check < errors.log

Migrating to a stricter tsconfig.json

To migrate the codebase to use a stricter tsconfig.json, you will need 2 tsconfigs:

  1. tsconfig.json - the strict tsconfig. This config will be used by the IDE and by the loose-ts-check tool.

    The aim is to see the errors in the IDE, to feel motivated to fix the errors while modifying existing files.

  2. tsconfig.loose.json - a tsconfig that extends tsconfig.json, but has the stricter options turned off.

    This config will be used by any linter, test runner, bundler you will have.

    If you are using ts-node, you need to pass tsconfig.loose.json as a TS_NODE_PROJECT variable, so it uses the correct tsconfig, e.g.

    cross-env TS_NODE_PROJECT="tsconfig.loose.json" webpack --mode=development

    To run tsc to do type-checking, pass -p tsconfig.loose.json option:

    tsc -p tsconfig.loose.json

Then, use the following command to initialize the config files:

tsc | npx loose-ts-check --init

After that, run

tsc | npx loose-ts-check

instead of tsc to do type-checking.

IDE plugin

In case you want to have the errors ignored in your IDE as well, use loose-ts-check-plugin.

Development

To verify the correctness, run:

npm run type-check
npm run lint:formatting
npm run test

Contributing

Contributions are welcome!

Make sure the CI passes on your PRs, and that your code is covered by unit tests.

changelog

Changelog

Unreleased

v2.0.0 (2023-04-01)

BREAKING CHANGES

  • --auto-update will no longer add new file paths to the registry

    When there were errors that could be ignored because the error code is already ignored in other files, --auto-update used to add these new file paths to the registry. This allowed regressions to sneak in, since a file that was already conforming to the stricter config could re-appear in the registry of ignored files.

    In situations like this one, these file paths will be no longer added to the registry, and the program will fail when there are errors in files that could be ignored.

    If you need to start ignoring these errors, use the --init flag to regenerate the registries.

    See https://github.com/Gelio/loose-ts-check/issues/16 for more information.

Engineering:

  • Include TypeScript 5.0 in integration tests.

v1.3.0 (2023-01-08)

Features:

  • Wildcard support in loosely-type-checked-files.json

    Paths in loosely-type-checked-files.json support wildcards now (e.g. src/**/* or node_modules/**/* or src/{a,b,c}/*.ts).

    Matching is done using minimatch.

Engineering:

  • Maintenance work (update npm packages, use newer Node versions in CI).
  • Add integration tests

    They give more confidence that the tool is working as expected.

v1.2.0 (2020-11-19)

Features:

  1. Display a list of all errors that cannot be ignored.

v1.1.0 (2020-11-11)

Features:

  1. Detect which ignored error codes specified in the config that did not occur.

    If --auto-update is passed in, those error codes are automatically removed from the config.

Engineering:

  1. Refactor the code for easier maintenance
  2. Add unit tests for the CLI and individual functions

v1.0.1 (2020-11-10)

Bugfix release

  1. Wait till the input ends before closing the input stream.

    This prevents displaying errors when piping output from a long-running TSC process when the loose-ts-check tool quits early due a config error.

v1.0.0 (2020-11-10)

Initial release of the tool.

Features:

  1. Parse TSC errors from standard input (stdin)
  2. --init CLI option to initialize the configuration files (ignored errors and loosely type-checked files)
  3. CLI options to customize the paths to configuration files
  4. --auto-update CLI option to automatically update the list of loosely type-checked files when some file from no longer needs errors to be ignored

Engineering:

  1. Some unit tests using jest
  2. CI using GitHub Actions
  3. Compilation using TypeScript
  4. Formatting using prettier