Skip to content

isValidSpreadType doesn't handle type parameter #20013

Closed

Description

TypeScript Version: 2.7.0-dev.20171114

Code

function shallowClone<T extends object>(x: T): T {
    return { ...x };
}

Expected behavior:

No error.

Actual behavior:

src/a.ts(2,14): error TS2698: Spread types may only be created from object types.

Nathan Shively-Sanders (@sandersn)

Activity

  1. sandersn commented on Nov 14, 2017

    @sandersn
    Member

    This is by design:

    1. ...T is not assignable to T
    class C {
      m() {
        return 'hi'
      }
    }
    shallowClone(new C()).m() // runtime error: 'm' is not a function
    1. Typescript doesn't have a spread type because (1) makes it unusable for React without some constraint on T.
  2. locked and limited conversation to collaborators on Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions