Skip to content

Object Properties Spread with Generic Object Types not allowed #15792

Closed
@kitsonk

Description

@kitsonk

TypeScript Version: 2.3.2

Code

Playground Example

class Foo<P> {
  setProperties(props: P) {
    const newProps = { ...props };
  }
}

class Bar<P extends object> {
  setProperties(props: P) {
    const newProps = { ...props };
  }
}

class Baz<P extends { [prop: string]: any; }> {
  setProperties(props: P) {
    const newProps = { ...props };
  }
}

class Qat<P extends object = object> {
  setProperties(props: P) {
    const newProps = { ...props };
  }
}

Expected behavior:

That properties are spread on class Bar, class Baz, and class Qat.

Actual behavior:

All three cases report that Spread types may only be created from object types. In all cases, the generic could be inferred to be of an object type, so the restriction seems to be arbitrary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions