π Search Terms
type narrowing inferred function parameter union discriminant
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about unions
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.9.3&ssl=17&ssc=52&pln=1&pc=1#code/JYOwLgpgTgZghgYwgAgGIHt3IN4ChnIBucANgK4QBcyAzmFKAOYDc+tEYAaqRdQBTFyVWvSYBKZAF4AfEXTAAJqwC+uXKEixEKAEJwoONoN7IQZALYAjaKwI0O3If2PCzV6BJlzFKtWACeAA4oAPKBYMDoIDRSaJjIAD7IelCsuDBkIAgRUcgwIHzo4ZHR1GE50RJ4BFAcZFAgpmQkJL7pBdhEPMIARD0ANOxc3fwQnrIIUTToJBAAdCTojHxjyMpizEA
π» Code
interface Foo {
value: string;
setValue: (value: string) => void;
}
interface Bar {
value: number;
setValue: (value: number) => void;
}
type Options = Foo | Bar;
function fn(options: Options) {
return null;
}
fn({ value: "", setValue: (e) => console.log(e) });
π Actual behavior
The e parameter of setValue is inferred as any rather than string, despite value being a string which means Foo is the only valid type.
π Expected behavior
setValue here should have e inferred as a string
Additional information about the issue
No response