Skip to content

Regression with unions introduced in typescript 4.2 #42935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dfilatov opened this issue Feb 24, 2021 · 3 comments
Closed

Regression with unions introduced in typescript 4.2 #42935

dfilatov opened this issue Feb 24, 2021 · 3 comments
Assignees
Labels
Bug A bug in TypeScript

Comments

@dfilatov
Copy link

dfilatov commented Feb 24, 2021

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

@ahejlsberg ahejlsberg self-assigned this Feb 24, 2021
@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label Feb 24, 2021
@ahejlsberg ahejlsberg removed their assignment Feb 24, 2021
@ahejlsberg ahejlsberg removed the Bug A bug in TypeScript label Feb 24, 2021
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Feb 24, 2021
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.3.0 milestone Feb 24, 2021
@sandersn
Copy link
Member

sandersn commented Apr 6, 2021

@ahejlsberg When you looked at this earlier, did you figure out whether this new error was working as intended?

I'll bisect when I have time if you didn't investigate this already.

Initial poking around shows that the repro requires --strict to succeed on 4.1 -- both 4.1 and 4.2 have the same error without strict.

@sandersn
Copy link
Member

sandersn commented Apr 6, 2021

Broken in #42233. @weswigham can you figure out if this was an intended break from that PR?

@sandersn sandersn assigned weswigham and unassigned sandersn Apr 6, 2021
@weswigham
Copy link
Member

weswigham commented Apr 6, 2021

Yeah, this was an intentional reduction in spread accuracy - typically there's a lot of exists-or-empty-object spreads like this in a single object (eg, for css or props in the wild), and so the higher accuracy spread logic just costs too much (has exponential check times).

@sandersn sandersn closed this as completed Apr 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants