• Stars
    star
    1,232
  • Rank 38,102 (Top 0.8 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 7 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A CLI tool to check type coverage for typescript code

type-coverage

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.

Dependency Status devDependency Status Build Status: Windows Github CI npm version Downloads type-coverage Codechecks

use cases

  • Show progress of long-term progressive migration from existing js code to typescript code.
  • Avoid introducing accidental any by running in CI.
  • Show progress of long-term progressive migration from existing looser typescript code to stricter typescript code.migrate to stricter typescript

install

yarn global add type-coverage typescript

usage

run type-coverage

arguments

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 < this value(Added in v1.0)
--debug boolean? show debug info(Added in v1.0)
--suppressError boolean? process exit 0 even failed or errored
--strict boolean? strict mode(Added in v1.7)
--ignore-catch boolean? ignore catch(Added in v1.13)
--cache boolean? enable cache(Added in v1.10)
--ignore-files string[]? ignore files(Added in v1.14)
-h, --help boolean? show help(Added in v2.5)
--is number? fail if coverage rate !== this value(Added in v2.6)
--update boolean? if "typeCoverage" section in package.json is present update its "atLeast" or "is" value (Added in v2.6)
--update-if-higher boolean? update "typeCoverage" in package.json to current result if new type coverage is higher(Added in v2.20)
--ignore-unread boolean? allow writes to variables with implicit any types(Added in v2.14)
--ignore-nested boolean? ignore any in type arguments, eg: Promise<any>(Added in v2.16)
--ignore-as-assertion boolean? ignore as assertion, eg: foo as string(Added in v2.16)
--ignore-type-assertion boolean? ignore type assertion, eg: <string>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 #113)
--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)

strict mode

If the identifiers' type arguments exist and contain at least one any, like any[], ReadonlyArray<any>, Promise<any>, Foo<number, any>, it will be considered as any too(Added in v1.7)

Type assertion, like foo as string, foo!, <string>foo will be considered as uncovered, exclude foo as const, <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)

Also, future minor release may introduce stricter type check in this mode, which may lower the type coverage rate

enable cache

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

ignore catch

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 = <Error> anyErr
}

To have the highest type coverage.

ignore files

This tool will ignore the files, eg: --ignore-files "demo1/*.ts" --ignore-files "demo2/foo.ts"

config in package.json

  "typeCoverage": {
    "atLeast": 99, // same as --at-least (Added in `v1.4`)
    "is": 99, // same as --is (Added in `v2.6`)
    "cache": true, // same as --cache (Added in `v2.11`)
    "debug": true, // same as --debug (Added in `v2.11`)
    "detail": true, // same as --detail (Added in `v2.11`)
    "ignoreCatch": true, // same as --ignore-catch (Added in `v2.11`)
    "ignoreFiles": ["demo1/*.ts", "demo2/foo.ts"], // same as --ignore-files "demo1/*.ts" --ignore-files "demo2/foo.ts" (Added in `v2.11`)
    "project": "tsconfig.json", // same as --project tsconfig.json or -p tsconfig.json (Added in `v2.11`)
    "strict": true, // same as --strict (Added in `v2.11`)
    "suppressError": true, // same as --suppressError (Added in `v2.11`)
    "update": true, // same as --update (Added in `v2.11`)
    "updateIfHigher": true, // same as --update-if-higher (Added in `v2.20`)
    "ignoreUnread": true, // same as --ignore-unread (Added in `v2.14`)
    "ignoreNested": true, // same as --ignore-nested (Added in `v2.16`)
    "ignoreAsAssertion": true, // same as --ignore-as-assertion (Added in `v2.16`)
    "ignoreTypeAssertion": true, // same as --ignore-type-assertion (Added in `v2.16`)
    "ignoreNonNullAssertion": true, // same as --ignore-non-null-assertion (Added in `v2.16`)
    "ignoreObject": true, // same as --ignore-object(Added in `v2.21`)
    "ignoreEmptyType": true, // same as --ignore-empty-type(Added in `v2.21`)
    "showRelativePath": true, // same as --show-relative-path (Added in `v2.17`)
    "historyFile": "typecoverage.json", // same as --history-file (Added in `v2.18`)
    "noDetailWhenFailed": true, // same as --no-detail-when-failed (Added in `v2.19`)
    "reportSemanticError": true, // same as --report-semantic-error (Added in `v2.22`)
    "cacheDirectory": "custom-directory", // same as --cache-directory (Added in `v2.24`)
    "notOnlyInCWD": true, // same as --not-only-in-cwd (Added in `v2.24`)
  },

ignore line

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
}

migrate to stricter typescript

Create a new tsconfig file(eg: tconfig.type-coverage.json) with stricter config, that extends from tsc's tsconfig.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "strict": true
  }
}

Run type-coverage -p ./tconfig.type-coverage.json --report-semantic-error, tsc semantic errors will be reported by type-coverage.

When all tsc semantic errors are resolved, merge the two tsconfigs.

add dynamic badges of type coverage rate

Use your own project url:

[![type-coverage](https://img.shields.io/badge/dynamic/json.svg?label=type-coverage&prefix=%E2%89%A5&suffix=%&query=$.typeCoverage.atLeast&uri=https%3A%2F%2Fraw.githubusercontent.com%2Fplantain-00%2Ftype-coverage%2Fmaster%2Fpackage.json)](https://github.com/plantain-00/type-coverage)

integrating with PRs

Using codechecks you can integrate type-coverage with GitHub's Pull Requests. See type-coverage-watcher.

type-coverage-watcher

typescript coverage report

Using typescript-coverage-report you can generate typescript coverage report.

typescript-coverage-report

API(Added in v1.3)

import { lint } from 'type-coverage-core'

const result = await lint('.', { strict: true })
export function lint(project: string, options?: Partial<LintOptions>): Promise<FileTypeCheckResult & { program: ts.Program }>
export function lintSync(compilerOptions: ts.CompilerOptions, rootNames: string[], options?: Partial<LintOptions>): FileTypeCheckResult & { program: ts.Program } // Added in `v2.12`

export interface LintOptions {
  debug: boolean,
  files?: string[],
  oldProgram?: ts.Program,
  strict: boolean, // Added in v1.7
  enableCache: boolean, // Added in v1.10
  ignoreCatch: boolean, // Added in v1.13
  ignoreFiles?: string | string[], // Added in v1.14
  fileCounts: boolean, // Added in v2.3
  absolutePath?: boolean, // Added in v2.4
  processAny?: ProccessAny, // Added in v2.7
  ignoreUnreadAnys: boolean, // Added in v2.14
  ignoreNested: boolean // Added in v2.16
  ignoreAsAssertion: boolean // Added in v2.16
  ignoreTypeAssertion: boolean // Added in v2.16
  ignoreNonNullAssertion: boolean // Added in v2.16
  ignoreObject: boolean // Added in v2.21
  ignoreEmptyType: boolean // Added in v2.21
  reportSemanticError: boolean // Added in v2.22
  cacheDirectory: string // Added in v2.24
  notOnlyInCWD?: boolean, // Added in v2.26
}

export interface FileTypeCheckResult {
  correctCount: number
  totalCount: number
  anys: FileAnyInfo[]
  fileCounts: { // Added in v2.3
    correctCount: number,
    totalCount: number,
  }[]
}

export interface FileAnyInfo {
  line: number
  character: number
  text: string
  kind: FileAnyInfoKind //  Added in v2.13
}

export const enum FileAnyInfoKind {
  any = 1, // any
  containsAny = 2, // Promise<any>
  unsafeAs = 3, // foo as string
  unsafeTypeAssertion = 4, // <string>foo
  unsafeNonNull = 5, // foo!
}

export type ProccessAny = (node: ts.Node, context: FileContext) => boolean

The typescript language service plugin of type-coverage(Added in v2.12)

ts-plugin demo

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 https://github.com/microsoft/TypeScript/wiki/Writing-a-Language-Service-Plugin#testing-locally, or just use the wrapped plugin below.

VSCode plugin(Added in v2.13)

https://marketplace.visualstudio.com/items?itemName=york-yao.vscode-type-coverage

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:

FAQ

Q: Does this count JavaScript files?

Yes, This package calls Typescript API, Typescript can parse Javascript file(with allowJs), then this package can too.

Changelogs

CHANGELOG for minor and patch release

v2

  1. Move typescript from dependencies to peerDependencies
  2. Move API from package type-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 })

More Repositories

1

schema-based-json-editor

A reactjs and vuejs component of schema based json editor.
TypeScript
168
star
2

tree-component

A reactjs and vuejs tree component.
TypeScript
146
star
3

types-as-schema

Genetate json schema, protobuf file, graphQL schema and reasonml/ocaml/rust types from typescript types.
TypeScript
40
star
4

ws-tool

A Develop Tool to Test WebSocket, Socket.IO, Stomp, Bayeux, HTTP, TCP, UDP, WebRTC, DNS API.
TypeScript
25
star
5

js-excel-template

A js excel template used in browser or nodejs environment.
TypeScript
24
star
6

select2-component

A vuejs and reactjs select component.
TypeScript
23
star
7

tour-component

A vuejs and reactjs tour component.
TypeScript
22
star
8

ws-heartbeat

Server-side and client-side heartbeat library for ws and browser-side Websocket.
TypeScript
18
star
9

blogs

something to share
JavaScript
16
star
10

file-uploader-component

A reactjs and vuejs component of file uploader.
TypeScript
12
star
11

ws-benchmark

A CLI tool for websocket, like apache bench for http.
TypeScript
12
star
12

relative-time-component

A auto-updated vuejs and reactjs relative time component.
TypeScript
10
star
13

package-dependency-graph

A CLI tool to generate a dependency graph of packages in a monorepo by graphviz or dagre.
TypeScript
10
star
14

ExcelFile.net

A Excel File operator based on NPOI.
C#
8
star
15

expression-engine

An expression tokenizer, parser and evaluator.
TypeScript
8
star
16

ts-csinterface

Adobe extensions CSInterface v7.0 library implementation in typescript (identical to original).
TypeScript
7
star
17

js-split-file

A library to split big file to small binary data for nodejs and browsers.
TypeScript
7
star
18

Bootstrap.Pagination

a Bootstrap pagination and pager for asp.net MVC and Webform
C#
7
star
19

rev-static

Static asset revisioning by appending content hash to filenames, then changing the names in html files.
TypeScript
7
star
20

no-unused-export

A CLI tool to check whether exported things in a module is used by other modules for Typescript.
TypeScript
7
star
21

deploy-robot

a test and deploy robot.
TypeScript
7
star
22

template-editor-demo

A poster template edit and generation design document and demo.
TypeScript
6
star
23

composable-editor-canvas

A composable editor canvas library.
TypeScript
6
star
24

clean-scripts

A CLI tool to make scripts in package.json clean.
TypeScript
4
star
25

rpc-on-ws

A lightweight RPC library on websocket connection.
TypeScript
4
star
26

code-count

A CLI tool to count code lines and characters.
TypeScript
4
star
27

monitor-a-list-from-redis

A simple tool to watch some realtime data from a redis source
TypeScript
3
star
28

weighted-picker

A library to pick a random item from weighted array.
TypeScript
3
star
29

grid-js-component

A reactjs and vuejs grid component.
TypeScript
3
star
30

optimize-yarn-lock

A CLI to optimize yarn.lock
TypeScript
3
star
31

simple-doc

A Server-less and Build-less markdown document application.
TypeScript
3
star
32

ps-extendscript-types

Photoshop extendscript typescript types.
TypeScript
3
star
33

markdown_to_pdf

A CLI tool to convert a markdown to a pdf file.
TypeScript
2
star
34

news-fetcher-client

The client side of a cross-platform tool to get and sync news.
TypeScript
2
star
35

protocol-first-design-demo

TypeScript
2
star
36

code-structure

A CLI tool to generate code structure for javascript or typescript source code.
TypeScript
2
star
37

prune-node-modules

A CLI tool to prune node_modules.
TypeScript
2
star
38

stringify2stream

A js library to stringify json to stream to avoid out-of-memory of JSON.stringify.
TypeScript
2
star
39

dns-protocol

A Library to encode and parse data for DNS protocol.
TypeScript
2
star
40

js-project-initializer

A tool to initialize a js project.
TypeScript
2
star
41

vscode-type-coverage

VSCode plugin for type-coverage. deprecated.
TypeScript
2
star
42

copy-tool

A tool to copy text or file from one place, and get it from another place
TypeScript
2
star
43

json-field-size

A CLI tool to calculate size of all fields in a json, for memory analysis.
TypeScript
1
star
44

ease-in-out

An ease-in-out no-css animation library.
TypeScript
1
star
45

image2base64-cli

A CLI tool to convert image file to base64 string.
TypeScript
1
star
46

Despise

a library to generate fake data for test, such as name, email, phone, number and so on.
C#
1
star
47

pagination-js-component

A vuejs and reactjs pagination component.
TypeScript
1
star
48

protocol-based-web-framework

A protocol and code generation based web framework.
TypeScript
1
star
49

DbHelper.Obsolete.Oracle

a oracle access helper.
C#
1
star
50

watch-then-execute

A CLI tool to execute script after source file changes.
TypeScript
1
star
51

git-commits-to-changelog

A CLI to generate changelog from git commits.
TypeScript
1
star
52

router-demo

Multiple-application SPA and SSR demo
TypeScript
1
star
53

Form.Recover

a library to recover form data from json(obsolete)
C#
1
star
54

queued-jobs

A library to handle jobs in a smooth way with help of queue for nodejs and browser
TypeScript
1
star
55

tab-container-component

A vuejs and reactjs tab container component.
TypeScript
1
star
56

badge-svg

A library to generate badge svg
TypeScript
1
star
57

reconnection

A javascript library for browser or nodejs client reconnection.
TypeScript
1
star
58

prerender-js

[Obsolete]A CLI tool to prerender a page and save html element by id to a file.
TypeScript
1
star