Skip to content

No error accessing private property with indexed access #26328

Closed

Description

TypeScript Version: 3.1.0-dev.20180809

Code

class A { private n!: number; }
const a = new A();
const nameN = "n";
const n = a[nameN];

Expected behavior:

Error at a[nameN] just like for a.n.

Actual behavior:

No error.

Activity

  1. ghost added
    BugA bug in TypeScript
    on Aug 9, 2018
  2. added
    Working as IntendedThe behavior described is the intended behavior; this is not a bug
    and removed
    BugA bug in TypeScript
    on Aug 9, 2018
  3. RyanCavanaugh commented on Aug 9, 2018

    @RyanCavanaugh
    Member

    This is an intended allowed bypass; see https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#413-property-access which outlines accessibility rules for a.x but not a["x"]

  4. ajafff commented on Aug 28, 2018

    @ajafff
    Contributor

    I wrote a linter rule to check accessibility of element access the same way TypeScript checks property access: no-restricted-property-access
    For a description on how to use it, see https://github.com/fimbullinter/wotan/blob/master/packages/wotan/README.md

  5. locked as resolved and limited conversation to collaborators on Oct 21, 2025
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

    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