A CLI tool to check type coverage for typescript code
A CLI tool to check type coverage for typescript code
A CLI tool to check type coverage for typescript code
This tool will check type of all identifiers, the type coverage rate = the count of identifiers whose type is not any / the total count of identifiers, the higher, the better.
any by running in CI.yarn global add type-coverage typescript
run type-coverage
| name | type | description |
|---|---|---|
-p, --project |
string? | tell the CLI where is the tsconfig.json(Added in v1.0) |
--detail |
boolean? | show detail(Added in v1.0) |
--at-least |
number? | fail if coverage rate foo(Added in v2.16`) |
--ignore-non-null-assertion |
boolean? | ignore non-null assertion, eg: foo!(Added in v2.16) |
--ignore-object |
boolean? | Object type not counted as any, eg: foo: Object(Added in v2.21) |
--ignore-empty-type |
boolean? | empty type not counted as any, eg: foo: {}(Added in v2.21) |
--show-relative-path |
boolean? | show relative path in detail message(Added in v2.17) |
--history-file |
string? | file name where history is saved(Added in v2.18) |
--no-detail-when-failed |
boolean? | not show detail message when the CLI failed(Added in v2.19)(Use --no-detail-when-failed=true to enable it ) |
--report-semantic-error |
boolean? | report typescript semantic error(Added in v2.22) |
-- file1.ts file2.ts ... |
string[]? | only checks these files, useful for usage with tools like lint-staged(Added in v2.23) |
--cache-directory |
string? | set cache directory(Added in v2.24) |
--not-only-in-cwd |
boolean? | include results outside current working directory(Added in v2.26) |
--json-output |
boolean? | output results as JSON(Added in v2.27) |
--report-unused-ignore |
boolean? | report unused ignore line directives, enabled if --strict(Added in v2.28) |
If the identifiers' type arguments exist and contain at least one any, like any[], ReadonlyArray, Promise, Foo, it will be considered as any too(Added in v1.7)
Type assertion, like foo as string, foo!, foo will be considered as uncovered, exclude foo as const, foo, foo as unknown(Added in v2.8), and other safe type assertion powered by isTypeAssignableTo(Added in v2.9)
Object type(like foo: Object) and empty type(like foo: {}) will be considered as any(Added in v2.21)
Strict mode enables --report-unused-ignore by default.
Also, future minor release may introduce stricter type check in this mode, which may lower the type coverage rate
save and reuse type check result of files that is unchanged and independent of changed files in .type-coverage directory(or set by --cache-directory), to improve speed
If you want to get 100% type coverage then try {} catch {} is
the largest blocked towards that.
This can be fixed in typescript with Allow type annotation on catch clause variable
but until then you can turn on --ignore-catch --at-least 100.
Your catch blocks should look like
try {
await ...
} catch (anyErr) {
const err = anyErr
}To have the highest type coverage.
This tool will ignore the files, eg: --ignore-files "demo1/*.ts" --ignore-files "demo2/foo.ts"
…Use type-coverage:ignore-next-line or type-coverage:ignore-line in comment(// or /* */) to ignore any in a line.(Added in v1.9)
try {
// type-coverage:ignore-next-line
} catch (error) { // type-coverage:ignore-line
}The --report-unused-ignore or --strict can report unused ignore line directives.(Added in v2.28)
…v2.12)yarn add ts-plugin-type-coverage -D
{
"compilerOptions": {
"plugins": [
{
"name": "ts-plugin-type-coverage",
"strict": true, // for all configurations, see LintOptions above
"ignoreCatch": true,
}
]
}
}For VSCode users, choose "Use Workspace Version", See , or just use the wrapped plugin below.
v2.13)Configuration is in Preferences - Settings - Extensions - Type Coverage
If the result from the vscode plugin is different from the result from the CLI, maybe your project root directory's tsconfig.json is different from your CLI tsconfig.json
If the plugin does not work, you may see some workarounds:
Q: Does this count JavaScript files?
Yes, This package calls Typescript API, Typescript can parse Javascript file(with allowJs), then this package can too.
Q: How to disable color in output?
Set the environment variable FORCE_COLOR=0 or use --no-color option, see chalk documentation for details.
CHANGELOG for minor and patch release
typescript from dependencies to peerDependenciestype-coverage to package type-coverage-core// v1
import { lint } from 'type-coverage'
lint('.', false, false, undefined, undefined, true)
// v2
import { lint } from 'type-coverage-core'
lint('.', { strict: true })No open issues yet, or sync has not completed.