-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Private member access and type information inconsistency. #1680
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
Comments
We have been discussing this on SO From what I know about TypeScript, it is meant to support normal JavaScript as correct syntax. This would mean that this specific example of accessing a private member by the object map should always be allowed. However, if --noImplicitAny exists, then we should also have a --strictTypes or something that forces all compiled code to use types and obey the rules. |
I can see how this feels weird. At the same time string indexing is generally an escape hatch for a variety of things. Likewise private isn't truly private and it feels a little weird to disallow this under |
Hi Dan, I think that is a fair assessment. If the type of the property was
It just seems strange to go as far as tracking that "private" is of type number, when unknown or untracked strings wouldn't do this (especially as it is supposed to be private - but I understand why you wouldn't want to enforce that). |
Looks like there is no more information needed here. closing. |
In section 8.2.2 Accessibility private members are not accessible outside of the class they are declared in.
However, you can access them using their key and when you do so, the type information is even inferred.
Here is the example:
a) Expected behaviour (error)
b) Should be error. If the compiler knows enough to say "it's a number", can it not also say "it is private"?
c) Expected behaviour (any)
d) Expected behaviour (number)
So in the case of variable b, should the private member not cause a compile-time warning?
The text was updated successfully, but these errors were encountered: