Test your TypeScript types easily.
This is a lighter version of tsd. Slightly reworked codebase allows tsd-lite
to be a tool which simply tests your types.
Note: This library is intended for programmatic use only. For CLI implementation see jest-runner-tsd
.
While tsd
suites perfectly for JavaScript libraries which declare their types in .d.ts
files, its usage with monorepos written in TypeScript may become cumbersome. tsd-lite
is an attempt to address these and similar issues.
tsd-lite
performs only type testing without any additional checks or rules.- Includes only type related assertions:
expectAssignable
,expectDeprecated
,expectType
and their counterparts. TheprintType
helper is removed. - Comes with no default compiler options.
- Reads TypeScript compiler options from the nearest
tsconfig.json
for each test file (does not read options frompackage.json
). tsd-lite
is optionallystrict
. You should add"strict": true
to the nearesttsconfig.json
(it can be project or test specific) to use strict assertions.tsdErrors
object is returned iftsd-lite
encounters errors while parsingtsconfig.json
or if syntax errors are found in the test files.@tsd/typescript
package is moved to peer dependencies.tsd-lite
allows only programmatic usage.
yarn add -D tsd-lite @tsd/typescript
Remember to install @tsd/typescript
. It is a required peer dependency.
import tsdLite from "tsd-lite";
const { assertionsCount, tsdResults, tsdErrors } = tsdLite(
"/absolute/path/to/testFile.test.ts"
);
The exported function takes fully resolved path to a test file as an argument and returns an object:
{
assertionsCount: number;
tsdResults: Array<{
messageText: string | ts.DiagnosticMessageChain;
file?: ts.SourceFile;
start?: number;
}>;
};
tsd-lite
will throw if TS compiler encountered an error while parsing tsconfig.json
or found a syntax error while compiling the code.