Is there a tool for #TypeScript that checks static type assertions similar to the following one?
// %inferred-type: (string | number | boolean)[]
const arr = [1, 'a', true];
I have written such a tool but it’s not ready for public consumption ATM, so such a tool already existing would save me the work of publishing what I have.
My tool is loosely similar to unit test runners such as Mocha—it enables static unit tests, if you will.
@rauschma definitely typed uses special comments compared textually and I would recommend tsd’s approach instead. The comment doesn’t look like a test to humans, there’s no editor support from typescript, and textual comparison causes tests to be fragile across typescript versions. You do get more precision—the ability to assert exactly what people will see in quickinfo.
(The rule is in Microsoft/definitelytyped-tools/packages/dtslint/src/rules/expectRule.ts)
@shivelysanders Good points, thanks!
I still miss one feature:
– In JS unit testing, there is assert.throws (or similar).
– My tool supports the equivalent for TS types—it compares text after @ ts-expect-error with the error message that it suppresses:
// @ts-expect-error: Type 'Person' is not assignable to type 'Color'.
// Types have separate declarations of a private property
// 'branded'. (2322)
const color: Color = person;
// Remaining code: https://exploringjs.com/tackling-ts/ch_class-related-types.html#switching-off-structural-typing
Any ideas?
@rauschma @shivelysanders I think this is something I could add to my type-model tests (ie., an ExpectNotType)
@rbuckton @shivelysanders
I’m wondering if that always works. I’ve collected how I use (a)ts-expect-error in my TypeScript book: https://gist.github.com/rauschma/4d1ee06e47e03545d4c4b31b59700786