Skip to content

Object spread incorrectly compiles getter #24799

Closed as not planned
Closed as not planned

Description

@dsehnal

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

Activity

  1. Buslowicz commented on Sep 21, 2018

    @Buslowicz

    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.

  2. SBhojani commented on Mar 26, 2020

    @SBhojani

    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 .

  3. guitarino commented on Jan 7, 2023

    @guitarino

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

  4. RyanCavanaugh commented on Jul 23, 2025

    @RyanCavanaugh
    Member

    Starting in 6.0 the minimum downlevel target will be ES6, which includes Object.assign, so there's no invocation of the spread helper anymore. Closing as moot.

  5. locked as resolved and limited conversation to collaborators on Jan 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptES2018Relates to the ES2018 Spec

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions