Skip to content

unsplash/ts-type-tests-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ts-type-tests-example

Video tutorial: https://www.youtube.com/watch?v=55OwY6RKMIw

A bare-bones example of how to test your types using the TSLint expect rule, provided by dtslint.

const add = (a: number, b: number): number => a + b;

add(1, 'oo') // $ExpectError
const result = add(1, 2) // $ExpectType number
tslint --project ./type-tests/ './type-tests/**/*.{ts,tsx}'

TypeScript version

By default, this TSLint rule runs expectations against typescript@next. However, we want it to use our local version of TS, so we must specify resolutions in our package.json.

Note: the logs will still show next, because of a hardcoded string. To avoid confusion, we patched this (using the excellent patch-package).

Separate TS project

Our tests live in a separate TS project to avoid tsc from erroring where we have an expected error ($ExpectError).

Show test errors in VS Code

Whilst developing, the TSLint extension for VS Code provides inline feedback. Note: this extension requires TSLint to be installed as a top level dependency. dtslint depends on TSLint, but it will not be hoisted to the top level of node_modules. For this reason we add TSLint as an explicit dependency.