Closed as not planned
Closed as not planned
Description
π Search Terms
"inference" "inference narrow" "inference discriminant" "inference union"
π Version & Regression Information
Tested on multiple versions, this is not a new bug.
β― Playground Link
π» Code
type Selection = {
multiple: true;
value: string[];
callback: (_: string[]) => void;
} | {
multiple: false;
value: string;
callback: (_: string) => void;
};
function test(a: Selection) {
// Error: Typescript does not recognise that `value` and `callback` use the same type.
a.callback(a.value);
if (a.multiple) {
// Valid: Typescript narrowed down the type to `string[]`
a.callback(a.value);
} else {
// Valid: Typescript narrowed down the type to `string`
a.callback(a.value);
}
}
π Actual behavior
Typescript does not recognise that callback
will always accept value
as a parameter, since they both use either string
or string[]
.
Interestingly, Typescript can infer the types properly using the if (multiple) { ... }
condition.
π Expected behavior
All three callback(value)
calls should be valid, from a type point of view.
Additional information about the issue
No response