tsd
Check TypeScript type definitions
Last updated 5 days ago by samverschueren .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install tsd 
SYNC missed versions from official npm registry.

tsd Build Status

Check TypeScript type definitions

Install

$ npm install tsd

Usage

Let's assume we wrote a index.d.ts type definition for our concat module.

declare const concat: {
	(value1: string, value2: string): string;
	(value1: number, value2: number): string;
};

export default concat;

In order to test this definition, add a index.test-d.ts file.

import concat from '.';

concat('foo', 'bar');
concat(1, 2);

Running npx tsd 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.

import {expectType} from 'tsd';
import concat from '.';

expectType<string>(concat('foo', 'bar'));
expectType<string>(concat(1, 2));

The tsd command will succeed again.

We change our implementation and type definition to return a number when both inputs are of type number.

declare const concat: {
	(value1: string, value2: string): string;
	(value1: number, value2: number): number;
};

export default concat;

If we don't change the test file and we run the tsd command again, the test will fail.

Strict type assertions

Type assertions are strict. This means that if you expect the type to be string | number but the argument is of type string, the tests will fail.

import {expectType} from 'tsd';
import concat from '.';

expectType<string>(concat('foo', 'bar'));
expectType<string | number>(concat('foo', 'bar'));

If we run tsd, we will notice that it reports an error because the concat method returns the type string and not string | number.

If you still want loose type assertion, you can use expectAssignable for that.

import {expectType, expectAssignable} from 'tsd';
import concat from '.';

expectType<string>(concat('foo', 'bar'));
expectAssignable<string | number>(concat('foo', 'bar'));

Top-level await

If your method returns a Promise, you can use top-level await to resolve the value instead of wrapping it in an async IIFE.

import {expectType, expectError} from 'tsd';
import concat from '.';

expectType<Promise<string>>(concat('foo', 'bar'));

expectType<string>(await concat('foo', 'bar'));

expectError(await concat(true, 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.

{
	"name": "my-module",
	"tsd": {
		"directory": "my-test-dir"
	}
}

Now you can put all your test files in the my-test-dir directory.

Custom TypeScript config

By default, tsd applies the following configuration:

{
	"strict": true,
	"jsx": "react",
	"target": "es2017",
	"lib": ["es2017"],
	"module": "commonjs",
	// The following option is set and is not overridable:
	"moduleResolution": "node"
}

These options will be overridden if a tsconfig.json file is found in your project. You also have the possibility to provide a custom config by specifying it in package.json:

{
	"name": "my-module",
	"tsd": {
		"compilerOptions": {
			"strict": false
		}
	}
}

Default options will apply if you don't override them explicitly. You can't override the moduleResolution option.

Assertions

expectType<T>(value)

Check that the type of value is identical to type T.

expectAssignable<T>(value)

Check that the type of value is assignable to type T.

expectNotAssignable<T>(value)

Check that the type of value is not assignable to type T.

expectError(function)

Check if the function call has argument type errors.

expectError<T>(value)

Check if a value is of the provided type T.

expectDeprecated(value)

Check that value is marked a @deprecated.

expectNotDeprecated(value)

Check that value is not marked a @deprecated.

Programmatic API

You can use the programmatic API to retrieve the diagnostics and do something with them. This can be useful to run the tests with AVA, Jest or any other testing framework.

import tsd from 'tsd';

(async () => {
	const diagnostics = await tsd();

	console.log(diagnostics.length);
	//=> 2
})();

tsd([options])

Retrieve the type definition diagnostics of the project.

options

Type: object

cwd

Type: string
Default: process.cwd()

Current working directory of the project to retrieve the diagnostics for.

License

MIT © Sam Verschueren

Current Tags

  • 0.6.5-beta                                ...           beta (4 years ago)
  • 0.6.5-beta.2                                ...           beta.2 (4 years ago)
  • 0.11.0                                ...           latest (5 days ago)
  • 0.6.0-beta.5                                ...           next (5 years ago)

35 Versions

  • 0.11.0                                ...           5 days ago
  • 0.10.0                                ...           a month ago
  • 0.9.0                                ...           2 months ago
  • 0.8.0                                ...           2 months ago
  • 0.7.4                                ...           4 months ago
  • 0.7.3                                ...           6 months ago
  • 0.7.2                                ...           8 months ago
  • 0.7.1                                ...           8 months ago
  • 0.7.0                                ...           8 months ago
  • 0.6.5 [deprecated]           ...           4 years ago
  • 0.6.5-beta.2                                ...           4 years ago
  • 0.6.5-beta                                ...           4 years ago
  • 0.6.4 [deprecated]           ...           4 years ago
  • 0.6.4-beta                                ...           4 years ago
  • 0.6.3 [deprecated]           ...           4 years ago
  • 0.6.1 [deprecated]           ...           4 years ago
  • 0.6.0 [deprecated]           ...           5 years ago
  • 0.6.0-beta.5                                ...           5 years ago
  • 0.6.0-beta.4                                ...           5 years ago
  • 0.6.0-beta.3                                ...           5 years ago
  • 0.6.0-beta.2                                ...           5 years ago
  • 0.6.0-beta.1                                ...           5 years ago
  • 0.5.7 [deprecated]           ...           6 years ago
  • 0.5.6 [deprecated]           ...           6 years ago
  • 0.5.5 [deprecated]           ...           6 years ago
  • 0.5.3 [deprecated]           ...           6 years ago
  • 0.5.2 [deprecated]           ...           6 years ago
  • 0.3.1 [deprecated]           ...           7 years ago
  • 0.3.0 [deprecated]           ...           7 years ago
  • 0.2.2 [deprecated]           ...           7 years ago
  • 0.2.1 [deprecated]           ...           7 years ago
  • 0.2.0 [deprecated]           ...           7 years ago
  • 0.1.2 [deprecated]           ...           7 years ago
  • 0.1.1 [deprecated]           ...           7 years ago
  • 0.1.0 [deprecated]           ...           7 years ago
Downloads
Today 32
This Week 32
This Month 772
Last Day 7
Last Week 389
Last Month 692
Dependencies (6)
Dev Dependencies (12)

Copyright 2014 - 2016 © taobao.org |