Skip to content

Private named instance fields are not removed from object spread #35943

Closed
@ajafff

Description

@ajafff

TypeScript Version: 3.8.0-dev.20191231

Search Terms:

Code

class C {
    #prop = 1;

    method(other: C) {
        const obj = { ...other };
        obj.#prop; // expected error, got no error
        const { ...rest } = other;
        rest.#prop; // expected error, got error
    }
}

Expected behavior:

Object spread should remove private named properties from the spreaded object type.

Actual behavior:

Private named properties are preserved on the spreaded type, accessing the property is not a type error. This causes a runtime error.

Note that object rest already works as expected.

Playground Link: https://www.typescriptlang.org/play/?ts=3.8.0-dev.20191231&ssl=10&ssc=2&pln=1&pc=1#code/MYGwhgzhAEDC0G8BQ1XQMQAcBOB7T0AvNAEwDcSKaAtgKYAuAFrgCYAUuTt2AXHAJSIqaNMFwA7CPWi4ARgCsiiaADo1nRt2gBfCiJFz5KrHkx79qMZOkJVa7LSk6lG7uYsOpxnPnc6k2kA

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions