Skip to content

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

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

Closed
4 of 5 tasks
TokugawaTakeshi opened this issue Mar 4, 2020 · 3 comments
Closed
4 of 5 tasks
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@TokugawaTakeshi
Copy link

TokugawaTakeshi commented Mar 4, 2020

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.
@TokugawaTakeshi TokugawaTakeshi changed the title Compile classes fields marked as "private" to ECMAScript private field (#xxx) Compile classes fields marked as "private" to ECMAScript private fields (#xxx) Mar 4, 2020
@nmain
Copy link

nmain commented Mar 4, 2020

Open discussion in #31670, including rationale for why this would not be done

@RyanCavanaugh RyanCavanaugh added Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript labels Mar 4, 2020
@RyanCavanaugh
Copy link
Member

Per above 👆 we won't be doing this

@TokugawaTakeshi
Copy link
Author

TokugawaTakeshi commented Mar 5, 2020

You have raised the holy war between private keyword followers and and ECMAScript private field followers 🙂 The mental energy consumption for the team work will increase 😕

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants