Skip to content

No check for access modifier for non-trivial class properties #35919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
danielrentz opened this issue Dec 30, 2019 · 3 comments
Closed

No check for access modifier for non-trivial class properties #35919

danielrentz opened this issue Dec 30, 2019 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@danielrentz
Copy link

TypeScript Version: 3.7.2

Search Terms: private computed property

Expected behavior:

TypeScript should check the access modifier of class properties with "weird" names that look like computed properties but are given literally (e.g. property names with dashes).

I understand that there is an intended bypass for "real" computed properties where the index is a non-constant expression (https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#413-property-access). But properties explicitly defined in the class should be checked when accessed literally (see playground example).

Actually, TypeScript even acts a little inconsistent: It fails if trying to access a class property that does not exist, but passes accessing private properties from outside.

Actual behavior:

TypeScript fails for undefined properties, but not for existing private properties.

Related Issues: #26328

Code

class C {
    public p1 = 1;
    private p2 = 1;
    public ["p-1"] = 1;
    private ["p-2"] = 1;
}

const c = new C;
console.log(c.p0); // error (ok)
console.log(c.p1);
console.log(c.p2); // error (ok)
console.log(c["p-0"]); // error (ok)
console.log(c["p-1"]);
console.log(c["p-2"]); // no error!
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Playground Link: Provided

@ajafff
Copy link
Contributor

ajafff commented Dec 30, 2019

How is this different from #26328?

@danielrentz
Copy link
Author

danielrentz commented Dec 30, 2019

From #26328: const n = a[nameN]; with nameN being a variable (well in real-life). This issue is specifically about object["literal"]. And ... about undefined vs. private properties behaving differently.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jan 8, 2020
@RyanCavanaugh
Copy link
Member

This is longstanding behavior to allow (ugh) the access of these properties. It'd be a very substantial breaking change at this point and we don't intend to invoke that pain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants