BugDomain: check: Control FlowHelp Wanted
仓库指标
- Star
- (48,455 star)
- PR 合并指标
- (平均合并 6天 17小时) (30 天内合并 9 个 PR)
描述
🔎 Search Terms
"union type", "narrowing", "inverted", "4.9"
🕗 Version & Regression Information
- This changed between versions 4.8.4 and 4.9.5 (probably caused by https://github.com/microsoft/TypeScript/pull/50397?)
⏯ Playground Link
💻 Code
interface Foo { foo: string; }
interface Bar { bar: string; }
interface Baz { baz: string; }
function myFunction(input: Foo | Bar | Baz) {
const isBaz = 'baz' in input;
const isBar =
//'bar' in input; // This works to narrow `bar` when true and narrow `foo` in the else block
!('foo' in input) && !isBaz; // This works to narrow `bar` when true but does not narrow `foo` in the else block
let x: string;
if (isBaz) {
x = input.baz;
} else if (isBar) {
x = input.bar;
} else {
// In 4.8.4 and earlier, `input` is narrowed to `Foo`
// In 4.9.5 and later, `input` is narrowed to `Foo | ((Foo | Bar) & Record<"baz", unknown>)`
x = input.foo;
}
}
🙁 Actual behavior
In 4.9.5 and later, in the else block, input.foo incorrectly narrows and errors
🙂 Expected behavior
In 4.8.4 and earlier, in the else block, input.foo correctly narrows
Additional information about the issue
No response