-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Nested conditional type with generic tuple argument always expands to false branch. #30708
Comments
Even smaller repro (perhaps not 'smaller'): type Inner<T> = [T] extends [true] ? false : true;
type Outer<T> = [Inner<T>] extends [true] ? 1 : 2; // Already resolved to 2. Most likely the wildcard instantiation for |
This is technically a duplicate of #30020 |
This issue is closed. just because someone provide a workaround. No one care the bug itself. it may cause some other problem which has not workaround. |
I found a similar issue while writing a conditional type // From this answer https://stackoverflow.com/a/52991061/374328
type OptionalKeys<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? K : never }[keyof T];
// Test if a single property on type T is optional
type IsOptional<T, K extends keyof T> = {} extends Pick<T, K> ? true : false;
// example interfaces
interface I {
x: string;
y?: number
}
type IX = IsOptional<I, 'x'> // false - ok
type IY = IsOptional<I, 'y'> // true - ok
interface J extends I {
y: number;
}
type JX = IsOptional<J, 'x'> // false - ok
type JY = IsOptional<J, 'y'> // false - ok
// issue
type TX<T extends I> = IsOptional<T, 'x'>; // expands to false - correct since x required in I
type TY<T extends I> = IsOptional<T, 'y'> // expands to false - incorrect - y might be optional in T
Finally, note that the following implementation of type IsOptional<T, K extends keyof T> = K extends OptionalKeys<T> ? true : false; |
Looks like the OP issue got resolved somewhere along the way, but similar issues keep cropping up (see SO question). Is there some more general bug report for this? Should we close this one and open a new one? |
TypeScript Version: next (
3.4.0-dev.20190330
), latest (3.4.1
)Search Terms:
Code
Expected behavior:
Type
t1
must befalse
andNoOp
must be of the nested conditional type that depends onT
, but not of simpletrue
unit type.Actual behavior:
As you see, something is broken when you use generic argument for tuple comparison in conditional type, because when you expand your types manually (
t0
is an expanded representation oft1
) it works as expected.Playground Link:
Klick me
Related Issues:
This bug report originated from my stackoverflow question.
Additional credits to @dragomirtitian and @fictitious for helping and localizing the bug to a minimal representation.
The text was updated successfully, but these errors were encountered: