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}'
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
).
Our tests live in a separate TS project to avoid tsc
from erroring where we have an expected error ($ExpectError
).
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.