-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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 types are evaluated too eagerly #34810
Comments
I also tried to define Foo differently: type Foo = ((arg1: 0, arg2: number) => void) & ((arg1: 1, arg2: string) => void); But that infers |
|
So is it somehow possible to get the wanted behavior? |
Not really, no. You'd need #33014 at an absolute minimum - there's currently no mechanism that would allow for the kind of narrowing implied here. |
Sad. Ok, thank you |
If I understand correctly, your intention is kind of like the following: the parameter let x: number;
type F = { 0: number, 1: string }
function foo<X extends 0 | 1>(arg1: X, arg2: F[X]): void {
if (arg1 === 0) {
x = arg2;
}
} Unfortunately, just #33014 won't solve your problem (it is similar to the rejected case foo(0 as 0 | 1, "oops"); Meaning: the value of We would additionally require something like #30284 to solve this. |
TypeScript Version: 3.7.x-dev.201xxxxx
Search Terms: conditional type eager, conditional type narrowing
Code
Expected behavior:
No errors,
t
has type0 | 1
in the function body which gets narrowed to just0
in the body of the if statement. In the if statement,d
should have type number, because we know thatt
has to have type0
.Actual behavior:
Type 'string | number' is not assignable to type 'string'.
Playground Link: https://www.typescriptlang.org/play/?ts=3.7-Beta#code/C4TwDgpgBAYg9nAEhANpATgHgBoD4oC8UAFAIboDmAjAFxTYA0U5FATHdlBAB7AQB2AEwDOUAAxQA-FGHB0AS34UodfgFcAtgCMI6AJSF8ANzjzBAbgBQoSLASE7SVBkwSAPlCq4rllBGBQ3HSyCkpWAMZw-LJQAGYIdPBwDsTATIIGBPgA3pZQ+VDysamEBERiBtmBDhZQAL6WdUA
Related Issues: not really
The text was updated successfully, but these errors were encountered: