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

inference problem with constraints involving conditionals and generics #29662

Closed
zpdDG4gta8XKpMCd opened this issue Jan 31, 2019 · 5 comments · Fixed by #30639
Closed

inference problem with constraints involving conditionals and generics #29662

zpdDG4gta8XKpMCd opened this issue Jan 31, 2019 · 5 comments · Fixed by #30639
Assignees
Labels
Fix Available A PR has been opened for this issue Needs Investigation This issue needs a team member to investigate its status.

Comments

@zpdDG4gta8XKpMCd
Copy link

zpdDG4gta8XKpMCd commented Jan 31, 2019

the following is seen in [email protected]:

declare function onlyNullablePlease<T extends (null extends T ? any : never)>(value: T): void;

declare var z: string | null;
onlyNullablePlease(z); // works as expected

declare var y: string;
onlyNullablePlease(y); // error as expected

function f<T>(t: T) {
    var x: T | null = Math.random() > 0.5 ? null : t;
    onlyNullablePlease(x); // should work, unexpected error:
    /*
Argument of type 'T | null' is not assignable to parameter of type 'null extends T | null ? any : never'.
  Type 'null' is not assignable to type 'null extends T | null ? any : never'.ts(2345)
    */
}
@jack-williams
Copy link
Collaborator

jack-williams commented Jan 31, 2019

I suspect this PR #29338 introduced the delta, not the one you link. I also think this is related to #29505. Wrapping the check and extends types in a 1-tuple is a short-term fix.

declare function onlyNullablePlease<T extends ([null] extends [T] ? unknown : never)>(value: T): void;

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Feb 5, 2019
@zpdDG4gta8XKpMCd
Copy link
Author

fyi, the problem is a major breaking change in our project, it prevents us from upgrading to the latest version of typescript, the workaround by @jack-williams helped though

@weswigham
Copy link
Member

weswigham commented Feb 27, 2019

Personal request: Use unknown instead of any for something like the example in the OP - type parameters extending any are currently.... strange (they're kinda checked and kinda not checked). See #29571 where we plan to fix the strangeness.

@zpdDG4gta8XKpMCd
Copy link
Author

unknown doesn't help

@weswigham
Copy link
Member

Oh, yeah, it doesn't help here, but I just wanted to inform that extends any behaves poorly and might want to be avoided, since extends unknown behaves far better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fix Available A PR has been opened for this issue Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
5 participants