Skip to content
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

Possible regression or breaking change in TS 4.3 beta #43758

Closed
markusjohnsson opened this issue Apr 21, 2021 Β· 1 comment
Closed

Possible regression or breaking change in TS 4.3 beta #43758

markusjohnsson opened this issue Apr 21, 2021 Β· 1 comment

Comments

@markusjohnsson
Copy link
Contributor

Bug Report

πŸ”Ž Search Terms

spread assignment generic constraint

πŸ•— Version & Regression Information

version 4.3.0-dev.20210418
version 4.3.0-beta

  • This changed between versions 4.2 and 4.3

⏯ Playground Link

Errors with ts 4.3.0-beta:
https://www.typescriptlang.org/play?ts=4.3.0-beta#code/FAFwngDgpgBAgjAvDA3jA1gSwHYBMBcMARAIZEwC+A3KJLAEJKoY4HEBG51t0MAwgHsAthAHYo2EEwQAfGPRrAAZgFdsAYxCYxMAE5R2KzABtcAHgAqMKAA8QE3AGd+w0eMkA+ABTrCFgJR+qMAwoTDqYo5S4gDufEHIKCFhKQB06erJodwp+iAqutgwsXw0FMBAA

Works fine with ts 4.2.3:
https://www.typescriptlang.org/play?ts=4.2.3#code/FAFwngDgpgBAgjAvDA3jA1gSwHYBMBcMARAIZEwC+A3KJLAEJKoY4HEBG51t0MAwgHsAthAHYo2EEwQAfGPRrAAZgFdsAYxCYxMAE5R2KzABtcAHgAqMKAA8QE3AGd+w0eMkA+ABTrCFgJR+qMAwoTDqYo5S4gDufEHIKCFhKQB06erJodwp+iAqutgwsXw0FMBAA

πŸ’» Code

type A = { kind: "a" };
type B = { kind: "b" };
type Component = A | B;

function rebuild<T extends Component>(c: T): T {
    const newC: T = { // <------- error: cannot assign
        ...c
    };
    return newC;
}

πŸ™ Actual behavior

Type '{ kind: "a"; } | { kind: "b"; }' is not assignable to type 'T'.
  '{ kind: "a"; } | { kind: "b"; }' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'Component'.
    Type '{ kind: "a"; }' is not assignable to type 'T'.
      '{ kind: "a"; }' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'Component'.(2322)

πŸ™‚ Expected behavior

No error, since c is T, { ... c } should also be T.

@MartinJohns
Copy link
Contributor

Duplicate of #43749.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants