-
Notifications
You must be signed in to change notification settings - Fork 12.8k
extends converts type of the form A | B to A & B #36445
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
This is working as intended, as per #29011. See the documentation for improved behavior for calling union types. The type |
You need to stop the type argument from being distributed by wrapping it in a tuple: type SlowBoy<Payload> = [Payload] extends [undefined] ?
(payload?: undefined) => Data<undefined> :
(payload: Payload) => Data<Payload>; |
Thanks @AlCalzone, that saves it from converting from |
TypeScript Version: 3.7.2
Search Terms: extends, type inference
Code
Key parts to look at in this code are the definition of
funcA
which specifies the type of the argument to beA | B
, but since it is typed asSlowboy<Payload>
it goes through anextends undefined
check which changes the expected argument type offuncA
fromA | B
toA & B
.Expected behavior:
I expect the
extends
keyword to check if thePayload
type extends undefined and if its the second case in which thePayload
doesn't extend undefined, thePayload
type should remain as applied in the definition offuncA
, that isA | B
.Actual behavior:
The
Payload
type applied in the definition offuncA
gets modified by theextends
type check fromA | B
toA & B
.Is this the expected behavior of extends? Cause I don't think the type checks done by
extends undefined
should stay after the check has already determined the type.Playground Link: https://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=37&pc=3#code/FAFwngDgpgBAgjAvDA3sGGYEsDOA5KAdwC4YQAnAVygG51MoAPCgQ1LU05gDsWBbKKRwUs3AOYAaelxgsxgnpT4AjKOWkwAvsG2hIsAEJJUG3ARIwAZiwA2OWjuB7o8APbkjyBAB8YBp+AuACIsICwAPAAqAHzGHJi8AkIi4nScECxgNq4sACakkTCOzrAAytmEBq5g4QAKmdl5scj1WTm5MEwgUNy5ODCUvVCWolAdAPwaABQZbXnjpIO5w6O5AJRIsSFh4Usr3GOxxNOzjfkwrWcbiFuhEZft0XROAMau3MJWgy9wpOWulWq4Tg7gMzRgMwa7VIII811iU3iGESCgARHBUVJ0lC8jo1k5LN84IjTPgiKQKNQsQxmOQ2CYZMj+AoAORYABWjBwWFyLOpMjkCgAzABGAAMGm0mjWQA
Hover over the
funcA
call and it shows the expected argument type to beA & B
Related Issues:
The text was updated successfully, but these errors were encountered: