Skip to content

narrowing is lost at creating literals #21436

Closed
@zpdDG4gta8XKpMCd

Description

@zpdDG4gta8XKpMCd
interface A { isIt: true; text: string; }
interface B { isIt: false; value: number; }
type C = A | B;
const isIt = Math.random() > 0.5;
const c : C = isIt ? { isIt, text: 'hey' } : { isIt, value: 123 }; /* <-- expected to work, actual:
Type '{ isIt: boolean; text: string; } | { isIt: boolean; value: number; }' is not assignable to type 'C'.
  Type '{ isIt: boolean; text: string; }' is not assignable to type 'C'.
    Type '{ isIt: boolean; text: string; }' is not assignable to type 'B'.
      Property 'value' is missing in type '{ isIt: boolean; text: string; }'.
*/

the following works tho:

const c : C = isIt ? { isIt: isIt, text: 'hey' } : { isIt: isIt, value: 123 };

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions