Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expectType generic causes incorrect inference #142

Open
mmkal opened this issue Jan 29, 2022 · 8 comments
Open

expectType generic causes incorrect inference #142

mmkal opened this issue Jan 29, 2022 · 8 comments

Comments

@mmkal
Copy link

mmkal commented Jan 29, 2022

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'`
@ilchenkoArtem
Copy link

I have this issue too

mmkal added a commit to mmkal/expect-type that referenced this issue Sep 7, 2022
tommy-mitchell added a commit to tommy-mitchell/tsd that referenced this issue Sep 14, 2022
@tommy-mitchell
Copy link
Contributor

Added a test. @sindresorhus do you have any idea how to go about fixing this? From the conversation in dot-prop it seems no, but it could be worth further looking into.

@sindresorhus
Copy link
Collaborator

do you have any idea how to go about fixing this?

No

@sindresorhus
Copy link
Collaborator

Added a test

Can you submit it it (commented out)?

@tommy-mitchell
Copy link
Contributor

By submit it do you mean as a PR?

@sindresorhus
Copy link
Collaborator

Yes

@tommy-mitchell
Copy link
Contributor

See #196 for discussion on this.

@SamVerschueren
Copy link
Collaborator

Given how TypeScript and generics work, I think currently the only way of solving this is by storing the result of the inferrable() call in a variable first

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

const val = inferrable();

expectType<number>(val);

TypeScript forces the generic to fit in the argument. And there doesn't seem to be a way to prevent this. It would be fixed by something like expectNumber() or expectString(). But if you have much more complex types, tsd can't offer an expectXXX for everything 🤷 . I'll think about a possible solution 🤔 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants