You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If an object literal type with only optional properties is part of a union, the union can't be narrowed.
I expected the bug to be common enough to have been already discussed somewhere, but the most relevant issue I could find was #31156. I apologise in advance if I'm opening a dupe.
🔎 Search Terms
narrowing union
🕗 Version & Regression Information
The bug can be found in any version of the TypeScript Playground (currently from v3.3.3333 to 4.6.2, including the Nightly v4.7.0-dev.20220427).
// Base case. It works as expected.typeCaseOne='A'|{optional: false}constf=(arg: CaseOne)=>{// `arg` is narrowed to `A` as expected.if(arg==='A')arg;}// Any object literal in the union has only optional properties.typeCaseTwo='A'|{optional?: true}|{anotherOne?: 2}constg=(arg: CaseTwo)=>{// No narrowing occurs.if(arg==='A')arg;}// The union has at least one object literal with at least one required property// and at least one object literal with only optional properties.typeCaseThree='A'|{optional?: true}|{anotherOne?: 2}|{dizzy: 'eh'}consth=(arg: CaseThree)=>{// Unreliable narrowing that depends on how you play with the question marks.if(arg==='A')arg;}
🙁 Actual behavior
Object literal types with only optional properties break control flow narrowing.
🙂 Expected behavior
It should be possible to narrow unions containing an object literal type with only optional properties.
The text was updated successfully, but these errors were encountered:
Hm, the issue stems from the fact that string literal types are comparable with {} and other weak object types. Comparability strikes again. @DanielRosenwasser do you know of a related/duplicate for this? Seems like the kind of issue that haunts you.
It's not really specific to comparability, this would happen with assignability too, right?
This does feel like a bug since we already special-case literal types in the negative case.
// Any object literal in the union has only optional properties.typeCaseTwo='A'|{optional?: true}|{anotherOne?: 2}constg=(arg: CaseTwo)=>{// Narrowing occurs.if(arg!=='A')arg;// Narrowing does not occur.elsearg}
Bug Report
If an object literal type with only optional properties is part of a union, the union can't be narrowed.
I expected the bug to be common enough to have been already discussed somewhere, but the most relevant issue I could find was #31156. I apologise in advance if I'm opening a dupe.
🔎 Search Terms
narrowing union
🕗 Version & Regression Information
The bug can be found in any version of the TypeScript Playground (currently from v3.3.3333 to 4.6.2, including the Nightly v4.7.0-dev.20220427).
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
Object literal types with only optional properties break control flow narrowing.
🙂 Expected behavior
It should be possible to narrow unions containing an object literal type with only optional properties.
The text was updated successfully, but these errors were encountered: