-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Incorrect assignability check when using Extract (TS2322) #46413
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 don't see where it's subtracting type A<T> = {
val: Extract<number | string, T>;
};
type An = A<number>; /*
= { val: Extract<number | string, number>; };
= { val: number; }; */
type An = A<number | string>; /*
= { val: Extract<number | string, number | string>; };
= { val: number | string; }; */ At worst, |
Sorry, my mistake, I got
|
It thinks that function f2(x: A<number | string>): A<number> {
return x;
}
This makes sense for general conditional types (consider The real bug here afaict is that TS is doing a variance check here instead of doing structural comparison; the variance measure should probably be tagged with |
Possibly related: #43887 |
Bug Report
🔎 Search Terms
extract assignable TS2322
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about Generics, Assignability, and Extract.
Tested with 4.2.3, 4.3.5, 4.4.4, and 4.5.0-dev.20211018.
In 4.1.5, the example using a type alias also fails. Version 4.2 introduced Smarter Type Alias Preservation, which may explain why that example passes starting from 4.2.x.
⏯ Playground Link
Playground link with relevant code
💻 Code
I have attempted to minimize this example as much as possible. The use of
Extract
to define a field type appears to be significant.🙁 Actual behavior
The type checker reports error TS2322:
A<number>
is not assignable to typeA<string | number>
becausestring | number
is not assignable tonumber
. It appears to be checking assignability of a field in the wrong direction, because the assignability check should be whethernumber
is assignable tostring | number
.Using a type alias for
A<number>
does not produce the same error. I was also not able to reproduce this error without usingExtract
to define a field type.🙂 Expected behavior
The type
A<number>
should be assignable to typeA<string | number>
because it is a simple object type andnumber
is assignable tostring | number
.The text was updated successfully, but these errors were encountered: