tsd-check
Check TypeScript type definitions
Install
$ npm install tsd-check
Usage
Let's assume we wrote a index.d.ts
type definition for our concat module.
declare ; ;
In order to test this definition, add a index.test-d.ts
file.
; concat'foo', 'bar';concat1, 2;
Running npx tsd-check
as a command will verify that the type definition works correctly.
Let's add some extra assertions. We can assert the return type of our function call to match a certain type.
;; expectTypeconcat'foo', 'bar';expectTypeconcat1, 2;
The tsd-check
command will succeed again.
We change our implementation and type definition to return a number
when both inputs are of type number
.
declare ; ;
If we don't change the test file and we run the tsd-check
command again, the test will fail.
await
Top-level If your method returns a Promise
, you can use top-level await
to resolve the value instead of wrapping it in an async
IIFE.
;; expectTypeconcat'foo', 'bar'; expectTypeawait concat'foo', 'bar'; expectErrorawait concattrue, false;
Test directory
When you have spread your tests over multiple files, you can store all those files in a test directory called test-d
. If you want to use another directory name, you can change it in package.json
.
Now you can put all your test files in the my-test-dir
directory.
Assertions
expectType<T>(value)
Check if a value is of a specific type.
expectError(function)
Check if the function call has argument type errors.
expectError<T>(value)
Check if a value is of the provided type T
.
License
MIT © Sam Verschueren