Skip to content

Error accessing private property in destructuring with computed property name #26356

@ajafff

Description

@ajafff

TypeScript Version: 3.1.0-dev.20180808

Search Terms: private string access

Code

class Foo {
    private bar = 1;
}
let foo = new Foo();

foo.bar; // error as expected
foo['bar']; // no error as expected

let {bar} = foo; // error as expected
let {'bar': bar2} = foo; // error (expected?)
let {['bar']: bar3} = foo; // error (expected?)

Expected behavior:

#26328 states that accessing non-public properties via element access with string literal or late-bound name is intentionally allowed.
To be consistent that should also apply to destructuring with computed property name or quoted name.

Actual behavior:

Errors as described by the inline comments.

Playground Link: https://agentcooper.github.io/typescript-play/#code/MYGwhgzhAEBiD29oG8BQ0PQA4CcCWAbmAC4Cm0ARmDtALzQCMA3KgL6oinHQBmid0AHakA7nEQAKAJQtUfeADoqOJtAD0a6KRw54NSFoAeWUsDIATOYgDaAcmW2Auqo1Ck23fpiljpi6g4uFGVWAXkXTQ89aAMfEzNSS05uZHtqWwAuSmoAJlD6cPVInWiJOL9EgH4pQJS7B0cs5QBmfN5ECK0SmjLfBPNqoA

Related Issues:
#26328
#26355

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issue

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions