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

Object Properties Spread with Generic Object Types not allowed #15792

Closed
kitsonk opened this issue May 12, 2017 · 6 comments
Closed

Object Properties Spread with Generic Object Types not allowed #15792

kitsonk opened this issue May 12, 2017 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@kitsonk
Copy link
Contributor

kitsonk commented May 12, 2017

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.

@mhegazy
Copy link
Contributor

mhegazy commented May 12, 2017

This is tracked by #10727 and fixed by #13288

@mhegazy mhegazy added the Duplicate An existing issue was already created label May 12, 2017
@kitsonk kitsonk closed this as completed May 13, 2017
@timocov
Copy link
Contributor

timocov commented May 13, 2017

@mhegazy but it works in 2.2.1 - it is blocker to updated to 2.3 for us :-(

@mhegazy
Copy link
Contributor

mhegazy commented May 13, 2017

@mhegazy but it works in 2.2.1 - it is blocker to updated to 2.3 for us :-(

Not sure what you mean by that, this has always been an error..

c:\test>type b.ts
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> {
  setProperties(props: P) {
    const newProps = { ...props };
  }
}

c:\test>node c:\releases\2.2.1\tsc.js --version
Version 2.2.1

c:\test>node c:\releases\2.2.1\tsc.js b.ts
b.ts(3,24): error TS2698: Spread types may only be created from object types.
b.ts(9,24): error TS2698: Spread types may only be created from object types.
b.ts(15,24): error TS2698: Spread types may only be created from object types.
b.ts(21,24): error TS2698: Spread types may only be created from object types.

c:\test>node c:\releases\2.2.2\tsc.js --version
Version 2.2.2

c:\test>node c:\releases\2.2.2\tsc.js b.ts
b.ts(3,24): error TS2698: Spread types may only be created from object types.
b.ts(9,24): error TS2698: Spread types may only be created from object types.
b.ts(15,24): error TS2698: Spread types may only be created from object types.
b.ts(21,24): error TS2698: Spread types may only be created from object types.

c:\test>node c:\releases\2.3.2\tsc.js --version
Version 2.3.2

c:\test>node c:\releases\2.3.2\tsc.js b.ts
b.ts(3,24): error TS2698: Spread types may only be created from object types.
b.ts(9,24): error TS2698: Spread types may only be created from object types.
b.ts(15,24): error TS2698: Spread types may only be created from object types.
b.ts(21,24): error TS2698: Spread types may only be created from object types.

do you mean spread in JSX?

@timocov
Copy link
Contributor

timocov commented May 13, 2017

Oh, sorry - yes, we have this error in JSX. I just have decided that is the same errors - the symptoms is very similar :)

@mhegazy
Copy link
Contributor

mhegazy commented May 13, 2017

That is tracked by #15742, fixed by #15789, and should be in TS 2.3.3 (out next week).

The fix should be in typescript@next already, give it a try and let us know if you are still running into issues.

@timocov
Copy link
Contributor

timocov commented May 15, 2017

@mhegazy For 2.3.3-insiders.20170512 this problem is gone, thank you!

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants