A CLI tool to check type coverage for typescript code
npx @tessl/cli install tessl/npm-type-coverage@2.29.0Type Coverage is a comprehensive CLI tool and library for measuring TypeScript type coverage in codebases. It calculates the percentage of identifiers that have explicit types versus 'any' types, enabling developers to track and improve type safety during progressive migration from JavaScript to TypeScript.
npm install -g type-coverage typescriptCLI usage (global installation):
type-coverageFor programmatic usage:
import { lint, lintSync, FileAnyInfoKind } from "type-coverage-core";CommonJS:
const { lint, lintSync, FileAnyInfoKind } = require("type-coverage-core");CLI usage:
# Basic type coverage check
type-coverage
# Check with minimum threshold
type-coverage --at-least 90
# Show detailed results
type-coverage --detail --at-least 80
# Use with specific project
type-coverage -p ./tsconfig.json --detailProgrammatic usage:
import { lint } from "type-coverage-core";
// Analyze project type coverage
const result = await lint("./", {
strict: true,
detail: true,
enableCache: true
});
console.log(`Coverage: ${result.correctCount}/${result.totalCount} (${(result.correctCount/result.totalCount*100).toFixed(2)}%)`);Type Coverage consists of two main components:
type-coverage-core) for integrating type coverage analysis into build pipelines and development toolsComprehensive CLI tool with 20+ options for measuring and reporting TypeScript type coverage. Supports thresholds, detailed reporting, caching, and integration with CI/CD pipelines.
type-coverage [options] [-- file1.ts file2.ts ...]Key CLI options:
-p, --project <string> Path to tsconfig.json
--at-least <number> Fail if coverage < this value
--detail Show detailed results
--strict Enable strict mode checking
--cache Enable result caching
--ignore-files <patterns> Ignore specific files/patterns
--json-output Output results as JSONCore library API for integrating type coverage analysis into custom tools, build systems, and development workflows.
function lint(
project: string,
options?: Partial<LintOptions>
): Promise<{
correctCount: number;
totalCount: number;
anys: AnyInfo[];
program: ts.Program;
fileCounts: Map<string, Pick<FileTypeCheckResult, 'correctCount' | 'totalCount'>>;
}>;
function lintSync(
compilerOptions: ts.CompilerOptions,
rootNames: string[],
options?: Partial<LintOptions>
): {
correctCount: number;
totalCount: number;
anys: Array<AnyInfo & { sourceFile: ts.SourceFile }>;
program: ts.Program;
fileCounts: Map<string, Pick<FileTypeCheckResult, 'correctCount' | 'totalCount'>>;
};
interface LintOptions {
debug: boolean;
strict: boolean;
enableCache: boolean;
ignoreFiles?: string | string[];
ignoreCatch: boolean;
ignoreUnreadAnys: boolean;
ignoreNested: boolean;
ignoreAsAssertion: boolean;
ignoreTypeAssertion: boolean;
ignoreNonNullAssertion: boolean;
ignoreObject: boolean;
ignoreEmptyType: boolean;
reportSemanticError: boolean;
reportUnusedIgnore: boolean;
fileCounts: boolean;
files?: string[];
cacheDirectory?: string;
notOnlyInCWD?: boolean;
}interface AnyInfo {
file: string;
line: number;
character: number;
text: string;
kind: FileAnyInfoKind;
}
interface FileTypeCheckResult {
correctCount: number;
totalCount: number;
anys: FileAnyInfo[];
}
interface FileAnyInfo {
line: number;
character: number;
text: string;
kind: FileAnyInfoKind;
}
enum FileAnyInfoKind {
any = 1, // any type
containsAny = 2, // Promise<any>
unsafeAs = 3, // foo as string
unsafeTypeAssertion = 4, // <string>foo
unsafeNonNull = 5, // foo!
semanticError = 6, // TypeScript semantic errors
unusedIgnore = 7 // Unused ignore directives
}