-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Aliasing a type in a conditional type changes the result #48936
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
Comments
I believe this boils down to an unsound variance check. TS detects type A = { x: 1, y?: 2 }
type B = { x: 1 }
const p: A = null! as B; // ok, B is assignable to A
const q: keyof B = null! as keyof A; // error, keyof A is not assignable to keyof B
type Q<T> = { x: keyof T };
type T1 = Q<A> extends Q<B> ? true : never; // true
type QA = Q<A>;
type T2 = QA extends Q<B> ? true : never; // never
|
Possibly a duplicate of #48070? |
IIUC #48070, or at least the draft fix that Anders put up, is specific to signatures. I think @tjjfvi nailed it. @ahejlsberg thoughts / triage help? |
Yes, @tjjfvi's analysis is correct. When relating two instantiations of the same generic type, we rely on variance the measured variance for that generic type. However, when an instantiation of some generic type is re-aliased (as in the The "fix" here would be to mark variance of types involving |
@ahejlsberg Thanks. If this is a design limitation of the implementation of the type checker, is it safe to say that the outlined expectations are valid and this will be fixed in the future? I'm asking because I reimplement the TypeScript type computation (see #47658) and I have to choose what behaviour I will implement. I could certainly copy this arguably unexpected inconsistency but would prefer to keep it clean. |
Bug Report
π Search Terms
any, alias, conditional type,
π Version & Regression Information
All versions are effected.
β― Playground Link
Playground Link
π» Code
π Actual behavior
t1
is true, andt2
is never.π Expected behavior
Both,
t1
andt2
should be the same and benever
, because{a: string} extends {a: 'id' | 'username'}
is false.The only difference is that the left value of extends is for
t2
an alias, but the same type as int1
.Interesting might be that
t2
was in v4.1true
as well, but started to get the right value in v4.2.Maybe related to #31295
The text was updated successfully, but these errors were encountered: