TypeScript pivate fields, ECMAScript private fields
Compile classes fields marked as "private" to ECMAScript private fields (#xxx).
In TypeScript 3.8, ECMAScript private fields has been added, but TypeScript class fields marked as "private" are sill being compiled to default public ECMAScript-fields.
Because ECMAScript private fields are more safe, developer forced to rewrite "private"-marked class fields to ECMAScript class fields.
class Application {
public examplePublicField1: string;
public examplePublicField2: string;
#examplePrivateField: string; // standing out and annoying
}
Well, even if I stop to use public keyword, still protected keyword exists:
class Application {
examplePublicField: string;
protected exampleProtectedClassField: string; // standing out and annoying
#examplePrivateField: string;
}
Compile private examplePrivateClassField to #examplePrivateClassField when TypeScript compiler's target option is ES2020. Otherwise compile private examplePrivateClassField to default public examplePrivateClassField.
Add compiler option like unifiedPrivate which provides above behavior when turned on.
class Application {
private privateClassField: stirng;
}
should be compiled to:
class Application {
constructor(example) {
this.#example = example;
}
}
My suggestion meets these guidelines:
Open discussion in https://github.com/microsoft/TypeScript/issues/31670, including rationale for why this would not be done
Per above 馃憜 we won't be doing this
You have raised the holy war between private keyword followers and and ECMAScript private field followers :slightly_smiling_face: The mental energy consumption for the team work will increase :confused:
Most helpful comment
Open discussion in https://github.com/microsoft/TypeScript/issues/31670, including rationale for why this would not be done