Skip to content

Regression with unions introduced in typescript 4.2Β #42935

@dfilatov

Description

@dfilatov

Bug Report

There's a regression with unions is introduced in typescript 4.2

πŸ”Ž Search Terms

union, spread, 4.2

πŸ•— Version & Regression Information

  • This changed between versions 4.1.5 and 4.2.2

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type A = {
    prop1?: undefined;
    prop2?: undefined;
} | {
    prop1: string;
    prop2?: string;
};

const b: A = { prop1: 'p1', prop2: 'p2' };

const a: A = {
    ...b ? b : {}
};

πŸ™ Actual behavior

Error:

const a: A
Type '{ prop1?: string | undefined; prop2?: string | undefined; }' is not assignable to type 'A'.
  Type '{ prop1?: string | undefined; prop2?: string | undefined; }' is not assignable to type '{ prop1: string; prop2?: string | undefined; }'.
    Types of property 'prop1' are incompatible.
      Type 'string | undefined' is not assignable to type 'string'.
        Type 'undefined' is not assignable to type 'string'.(2322)

πŸ™‚ Expected behavior

No errors

ts 4.1.5 works as expected, without errors

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions