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

Axel Rauschmayer

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.

@vbraun Looks good, thanks! A different style than what I’m used to, with different pros and cons.

@rauschma this is different than `satisfies`, right?

@rauschma It sounds like the new satisfies operator does what you need (new in v4.9):

> const arr = [1, 'a', true];
undefined
> arr satisfies (string | number | boolean)[]
{{{no typescript error}}}

> const arr2 = [1, 'a', true, new Date()];
undefined
> arr2 satisfies (string | number | boolean)[];
<repl>.ts:8:16 - error TS1360: Type '(string | number | boolean | Date)[]' does not satisfy the expected type '(string | number | boolean)[]'.

@vincentrolfs Alas, it’s not strict enough:

const a = 123;
(a satisfies number | boolean | string); // no error

@vincentrolfs Yes, that looks similar to some of the other suggestions in the replies! I’ll have to think about it—different from what I’m currently doing, with different pros and cons.

@rauschma So basically, this: (typescript is so cool)

@rauschma

And you can create a generic type that would do the same job, by the way.

I have it in my work chats, can dig it up if you want.

It relies on basically putting a variable of a type X as an argument into a function. But with a creative twist you don't actually call any function, just create a type of it — which falls apart if type doesn't match. Errors are even quite useful.

@rauschma

Of course `satisfies` is a one-sided check: type X can pass as Y, but not vice versa.

I bet you can do both ways too, in clunkier syntax.

@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

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

@rauschma you can add the explicit type to the definition ala `const arr: (number | string | boolean)[] = [1, 'a', true]`, or if you add `as const` ala `const arr = [1, 'a', true] as const`, you can then use the new `satisfies` keyword with more precision.

(But I may be misunderstanding what you're after?)