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

Getter on class is present in inferred type from object-spreading instance #26279

Closed
andrewbranch opened this issue Aug 7, 2018 · 0 comments
Closed
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@andrewbranch
Copy link
Member

TypeScript Version: 3.1.0-dev.20180807

Search Terms:

  • object spread own enumerable
  • object spread accessor
  • getter object spread

Code (playground)

class Phone {
  get number() {
    return 1234;
  }

  getNumber() {
    return 1234;
  }
}

const phoneInstance = new Phone();
const phoneClone = { ...phoneInstance };

Expected behavior:
The inferred type for phoneClone should be {}

Actual behavior:
The inferred type for phoneClone is { number: number }

Playground Link: playground

Related Issues:


Discussion

  1. I realize that spreading a class instance is not desirable.
  2. I also realize (after searching for dupes and reading similar issues for a couple hours) that there are design limitations around TS not tracking own/enumerable properties.

But, from what I understand, it makes a guess and correctly omits methods on classes from the inferred type of spreading an instance of that class. Since getters on classes are also on the prototype, it seems like inconsistent behavior that methods are omitted from the inferred type but get/set accessors are not. Even though the compiler doesn’t really track what properties are own enumerable, it seems like it could use similar heuristics to determine, in this simple case, that the getter won’t be present on the spread object.

P.S. – thanks for your time, and apologies in advance if this does turn out to be a dupe that I couldn’t find 😬

@ghost ghost added Duplicate An existing issue was already created Bug A bug in TypeScript and removed Duplicate An existing issue was already created labels Aug 7, 2018
@ghost ghost added the Fixed A PR has been merged for this issue label Aug 8, 2018
@RyanCavanaugh RyanCavanaugh assigned ghost Aug 8, 2018
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.1 milestone Aug 8, 2018
@ghost ghost closed this as completed in #26287 Aug 9, 2018
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

2 participants