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

Conditional type inference does not work with a function's generic argument #27530

Closed
samijaber opened this issue Oct 3, 2018 · 2 comments
Closed
Labels
Question An issue which isn't directly actionable in code

Comments

@samijaber
Copy link

TypeScript Version: 3.0.0-dev.201xxxxx

Search Terms: conditional type inference generic

Code

type Get<T> = T extends {} ? true : false;

const fn = <T extends {}>(t: T) => {
    // Expected `true`
    // Actual `T extends {} ? true : false`
    type Result = Get<T>;
}

Expected behavior: correct type inference: type Result = true

Actual behavior: type inference not performed at all

Playground Link: link

@jack-williams
Copy link
Collaborator

jack-williams commented Oct 3, 2018

This conditional type is distributive because there is a naked type parameter in the check clause. A distributive conditional type will evaluate to never when the check type is instantiated to never, so it is not possible to immediately resolve this to true.

To get the desired behaviour make the conditional type non-distributive like so:

type Get<T> = [T] extends [{}] ? true : false;

const fn = <T extends {}>(t: T) => {
    const x: Get<T> = true; // ok, Get<T> = true
}

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Oct 3, 2018
@samijaber
Copy link
Author

Ah, I see. Thanks @jack-williams! Will close this then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants