Skip to content

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

Closed
@andrewbranch

Description

@andrewbranch

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 😬

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions