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 spread incorrectly compiles getter #24799

Open
dsehnal opened this issue Jun 8, 2018 · 3 comments
Open

Object spread incorrectly compiles getter #24799

dsehnal opened this issue Jun 8, 2018 · 3 comments
Labels
Bug A bug in TypeScript ES2018 Relates to the ES2018 Spec
Milestone

Comments

@dsehnal
Copy link

dsehnal commented Jun 8, 2018

TypeScript Version: 2.9.1

Search Terms:
object spread getter

Code

const A = { a: 1 };
const X = { ...A, get x() { return this.a } };
const Y = { a: A.a, get x() { return this.a } };
console.log(X.x, Y.x)

Expected behavior:
1 1

Actual behavior:
undefined 1

The proposal says that get x() should not be called (section Getters on the Object Initializer).

Executing the code in Chrome (66) console produces the expected behavior.

Playground Link:
link

@mhegazy mhegazy added Bug A bug in TypeScript ES2018 Relates to the ES2018 Spec labels Jun 8, 2018
@mhegazy mhegazy added this to the Future milestone Jul 13, 2018
@Buslowicz
Copy link

Same issue happens with Babel. The solution would be to use Object.getOwnPropertyDescriptors() to get properties with their values and/or getters/setters, instead of just iterating over properties.

@SBhojani
Copy link

SBhojani commented Mar 26, 2020

I just came across this one. Here's another piece of code to demonstrate the issue:

var x = {
  ...{}, 
  get r(){
    return Math.random();
  }
};
console.log(x.r);
console.log(x.r);
console.log(x.r);

In JS that logs 3 different random values. In TS, it logs a single random value thrice.

More at https://stackoverflow.com/questions/60861329/typescript-bug-this-difference-between-get-syntax-and-definepropertys-g .

@guitarino
Copy link

Just ran into this issue; it still exists in 4.9.4. Babel TypeScript works correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript ES2018 Relates to the ES2018 Spec
Projects
None yet
Development

No branches or pull requests

5 participants