fosstodon.org is one of the many independent Mastodon servers you can use to participate in the fediverse.
Fosstodon is an invite only Mastodon instance that is open to those who are interested in technology; particularly free & open source software. If you wish to join, contact us for an invite.

Administered by:

Server stats:

10K
active users

Is there a tool for 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: exploringjs.com/tackling-ts/ch

Any ideas?

exploringjs.comClass-related types • Tackling TypeScript
Ron Buckton :typescript:

@rauschma @shivelysanders I think this is something I could add to my type-model tests (ie., an ExpectNotType)