microsoft / microsoft/TypeScript

Object literal getters and setters doesn't work after spread syntax (ES2017 and below)

Open
#45,531 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: JS Emit Rescheduled
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

The getters/setters will become normal/plain properties if a spread assignment is used before them.

🔎 Search Terms

Getter, setter, spread

🕗 Version & Regression Information

4.3.5

⏯ Playground Link

Playground link with relevant code

💻 Code
var Foo = {
    key: 'value'
}
var Bar = {
    ... Foo,  // this breaks the getters/setters after it.    
    get name() { console.log(9); return this._name },
    set name(name) { this._name = `1${name}`},
    _name: '',
}
🙁 Actual behavior

name setter and getter on the Bar object are “removed” and replaced with a plain name property, given that after compilation, the method used is Object.assign to piece the objects together.

🙂 Expected behavior

Getters and setters on Bar should still remain.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Playground and the object-literal spread example, then inspect the ES2017-and-below emit that uses Object.assign. Compare the generated behavior with the expected getter and setter behavior; done means accessors on Bar remain functional after compilation.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.