-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Description
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 be A | B, but since it is typed as Slowboy<Payload> it goes through an extends undefined check which changes the expected argument type of funcA from A | B to A & B.
type A = {
isNew: true;
extra: {
name: string,
age: number
}
}
type B = {
isNew: false;
}
type AorB = A | B
type Data<T> = {
name: string;
payload: T
}
type SlowBoy<Payload> = Payload extends undefined ?
(payload?: undefined) => Data<undefined> :
(payload: Payload) => Data<Payload>;
const funcA: SlowBoy<AorB> = (payload: AorB) => ({
name: "A",
payload
})
funcA({ //Shows the expected type of argument to funcA as A & B
isNew: true,
extra: {
name: 'ijxsid',
age: 310
}
})Expected behavior:
I expect the extends keyword to check if the Payload type extends undefined and if its the second case in which the Payload doesn't extend undefined, the Payload type should remain as applied in the definition of funcA, that is A | B.
Actual behavior:
The Payload type applied in the definition of funcA gets modified by the extends type check from A | B to A & 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.
Hover over the funcA call and it shows the expected argument type to be A & B
Related Issues: