Skip to content

Spread operator does not call property getters #26547

Closed
@andreashuber69

Description

@andreashuber69

Search Terms:
spread operator, property getter

Code

interface IWhatever {
    readonly value: number;
}

class Whatever implements IWhatever {
    public get value() {
        return 42;
    }
}

const whatever: IWhatever = new Whatever();
const obj = { ...whatever, otherValue: 43 };
console.log(obj);

Expected behavior:
{ value: 42, otherValue: 43 }

This is expected because a readonly property of an interface can be implemented with a getter. The code using the interface cannot know that the property is implemented with a getter and therefore expects to see the property of the interface in the result.

Actual behavior:
{ otherValue: 43 }

Playground Link:
Link

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions