-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Inability to use typeof on an ES private property #47595
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
I wouldn't mind giving it a shot with a PR, but just to see if I'm at least on the right track before I start going down that path:
Would this be the correct way to get the feature to work? |
@RyanCavanaugh Do we need to support this syntax? const p2: Example['#privateProp'] = 'world'; TypeScript/tests/cases/conformance/classes/members/privateNames/privateNamesAndIndexedAccess.ts Lines 12 to 13 in 3206df8
class C {
#a = 1; // number
["#a"] = ""; // string
constructor() {
const a: C["#a"] = 1; // Should the checker use a number or a string?
}
}
If we allow TypeScript/tests/cases/conformance/classes/members/privateNames/privateNameAndIndexSignature.ts Line 10 in eb3e7bd
|
|
already in use with public properties. class Example {
#privateProp = 'hello';
['#publicProp']: number
}
type Prop = Example['#publicProp'] |
@a-tarasyuk Since |
For now I would not support indexed access lookup (
|
that would make sense and that's pretty much the same how |
I posted this on #48640, but reposting here as well:
And that's if we decide to go that far. Given @RyanCavanaugh's reticence, we might decide it's not worth the complexity it would add. |
The following syntax is currently illegal in TypeScript, but might work in the future [1]: const p: typeof this.#privateProp = 'world'; Support the syntax. [1] microsoft/TypeScript#47595
Bug Report
π Search Terms
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
The example is kept short for reproduction, but in practice this is more for cases where you would want to use
keyof this.#someProperty
and similar constructs (since here the type could just be inferred).π Actual behavior
Specifying a type using
const name: typeof this.#anESprivateProperty
is not possible.For
p
, the error is that an identifier is expected (because it's aPrivateIdentifier
and not anIdentifier
from the compiler's pov?).For
p2
, the error is that the property does not exist on the type, which is logical when done from "outside" the class where the property should not be visible.π Expected behavior
Having the ability to use typeof on an ES private property from "inside" the class itself where that property is visible, e.g. like in the example code above.
The text was updated successfully, but these errors were encountered: