Skip to content
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

private class property is unprotected if key is numeric #50485

Closed
pushkine opened this issue Aug 27, 2022 · 6 comments
Closed

private class property is unprotected if key is numeric #50485

pushkine opened this issue Aug 27, 2022 · 6 comments
Labels
Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it

Comments

@pushkine
Copy link
Contributor

Bug Report

πŸ•— Version & Regression Information

ts stable & nightly

⏯ Playground Link

Playground Link

πŸ’» Code

class Foo {
	private readonly r1337 = 1337;
	private readonly 1337 = 1337;
}

const foo = new Foo();

foo.r1337 = 
  foo.r1337 + 1;

foo[1337] = 
  foo[1337] + 1;

πŸ™ Actual behavior

foo.r1337 = 
//  ^^^^^        ERROR: Cannot assign to 'r1337' because it is a read-only property.
  foo.r1337 + 1;
//    ^^^^^      ERROR: Property 'r1337' is private and only accessible within class 'Foo'.

foo[1337] = 
//  ^^^^         ERROR: Cannot assign to  '1337' because it is a read-only property.
  foo[1337] + 1;
//    ^^^^       (No error)

Writing foo[1337] is denied because it is a readonly class property.
Reading foo[1337] is allowed although it is a private class property.

πŸ™‚ Expected behavior

foo[1337] = 
//  ^^^^         Cannot assign to '1337' because it is a read-only property.
  foo[1337] + 1;
//    ^^^^       ERROR: Property  '1337' is private and only accessible within class 'Foo'.

Reading foo[1337] throws an error because it is a private class property.

@whzx5byb
Copy link

This is an intended behavior. See #19335.

@pushkine
Copy link
Contributor Author

pushkine commented Aug 27, 2022

This is an intended behavior. See #19335.

I am aware of how the indexed access notation works, this issue is specifically about numeric properties.

I believe that the computed property notation is an escape hatch. It is not intended to forbid class properties from using the privacy feature when their key is numeric.

A numeric property can only be accessed using the bracket notation. I am not opting-in to the indexed access, I'm not accessing it using foo[i] or foo["1337"] . I declared it using a bracket-less notation private 1337 = 1337;, not private "1337" or private [1337]. I did all that in a class without indexed properties and I tried using every related compiler option under the sun. Still that property won't be private. I do not think that forbidding numeric keys from being private is intended.

@whzx5byb
Copy link

A numeric property can only be accessed using the bracket notation.

Here is the limitation: Any property access inside square brackets will ignore protected or private modifiers. The compiler doesn't distinguish between foo[i] and foo["1337"] and foo[1337].

@pushkine
Copy link
Contributor Author

pushkine commented Aug 29, 2022

The fact that Typescript allows making that property private, but actually never enforces its privacy is a bug.

It should either not allow privacy modifiers on numeric properties because they're defacto not supported or it should enforce privacy when accessing that property.

@RyanCavanaugh RyanCavanaugh added the Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it label Aug 29, 2022
@RyanCavanaugh
Copy link
Member

It should either not allow privacy modifiers on numeric properties because they're defacto not supported or it should enforce privacy when accessing that property.

Numeric names are certainly very rare and breaking someone's program on purpose to fix the perceived inconsistency doesn't seem like a great trade-off here.

@parzhitsky
Copy link

As came up in #60974 (comment), symbol keys also suffer from the same thing. My intuition is that they are much more widely used than numeric keys. Do we have the same policy for them as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it
Projects
None yet
Development

No branches or pull requests

4 participants