Skip to content

Compile classes fields marked as "private" to ECMAScript private fields (#xxx) #37206

Closed
@TokugawaTakeshi

Description

@TokugawaTakeshi

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:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions