Typescript: Compile classes fields marked as "private" to ECMAScript private fields (`#xxx`)

Created on 4 Mar 2020  路  3Comments  路  Source: microsoft/TypeScript

Search Terms

TypeScript pivate fields, ECMAScript private fields

Suggestion

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.

Problem 1: The developers forced to rewrite their code to new syntax

Because ECMAScript private fields are more safe, developer forced to rewrite "private"-marked class fields to ECMAScript class fields.

Problem 2: Syntax unity loss

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;
}

Solution alternative 1

Compile private examplePrivateClassField to #examplePrivateClassField when TypeScript compiler's target option is ES2020. Otherwise compile private examplePrivateClassField to default public examplePrivateClassField.

Solution Alternative 2

Add compiler option like unifiedPrivate which provides above behavior when turned on.

Examples

class Application {
    private privateClassField: stirng;
}

should be compiled to:

class Application {
   constructor(example) {
        this.#example = example;
    }
}

Checklist

My suggestion meets these guidelines:

  • [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [x] This wouldn't change the runtime behavior of existing JavaScript code
  • [x] This could be implemented without emitting different JS based on the types of the expressions
  • [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [ ] This feature would agree with the rest of TypeScript's Design Goals.
Declined Suggestion

Most helpful comment

Open discussion in https://github.com/microsoft/TypeScript/issues/31670, including rationale for why this would not be done

All 3 comments

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:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kyasbal-1994 picture kyasbal-1994  路  3Comments

wmaurer picture wmaurer  路  3Comments

dlaberge picture dlaberge  路  3Comments

siddjain picture siddjain  路  3Comments

Roam-Cooper picture Roam-Cooper  路  3Comments