Definitelytyped: Tests for code that *doesn't* compile?

Created on 8 Aug 2017  路  2Comments  路  Source: DefinitelyTyped/DefinitelyTyped

  • [x] I have a question that is inappropriate for StackOverflow. (Please ask any appropriate questions there).

@andy-ms

Has there been any discussion of adding a test file with code that doesn't compile? Let's say I write the types for a package foo in some way like this:

export function bar(arg: number);

I would like to be able to add a test like this

import { bar } from 'foo';

bar('wrong'); // tsc-error: Argument of type .* is not assignable to parameter of type 'number'

Where the comment signifies that there should be an error matching the regex provided.

This would help make sure that the type definitions are actually useful - that they prevent invalid use of the library.

Should be possible using the api shipped with typescript.

Most helpful comment

There is some functionality like that available in dtslint, so as long as you're linting your code (enabled by adding a tslint.json containing { "extends": "dtslint/dt.json" }) you can add // $ExpectError to a line where you expect an error.

All 2 comments

There is some functionality like that available in dtslint, so as long as you're linting your code (enabled by adding a tslint.json containing { "extends": "dtslint/dt.json" }) you can add // $ExpectError to a line where you expect an error.

Good enough for me!

Was this page helpful?
0 / 5 - 0 ratings