tsdjs / tsd

Check TypeScript type definitions
MIT License
2.4k stars 67 forks source link

`expectType` Generic Discussion Opener #163

Open tommy-mitchell opened 2 years ago

tommy-mitchell commented 2 years ago

From #142:

expectType doesn't work properly with generic functions. Here's a repro:

import {expectType} from 'tsd'

declare const inferrable: <T = 'SomeDefaultValue'>() => T

expectType<number>(inferrable()) // passes, should fail

I'm not sure if it's possible to fix given the API design, but here's the same test using expect-type which fails as it should.

import {expectTypeOf} from 'expect-type'

declare const inferrable: <T = 'SomeDefaultValue'>() => T

expectTypeOf(inferrable()).toEqualTypeOf<number>() // fails as expected, because `inferrable()` returns type `'SomeDefaultValue'`

Adds a reproduction test case:

// expect-error/generic/index.d.ts

export const inferrable: <T = 'SomeDefaultValue'>() => T;
// expect-error/generic/index.test-d.ts

expectError(expectType<number>(inferrable<true>())); // fails
expectError(expectType<number>(inferrable())); // should fail, doesn't
skarab42 commented 1 year ago

FI: This test pass with the new API proposal #168