Skip to content

Possible regression or breaking change in TS 4.3 betaΒ #43758

Closed
@markusjohnsson

Description

@markusjohnsson

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions