microsoft / microsoft/TypeScript
Account for discrepancy between soft privacy of `private` vs hard privacy of `#`
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
With the upcoming TC39 class fields proposal landing in 2022, there is a soft private vs hard private discrepancy between vanilla JS' # and TypeScript's private. This discrepancy might not be obvious to all users and could cause issues.
Maybe the TS docs should highlight this discrepancy, or maybe we should have some kind of warning. Alternatively (and this would have to be raised elsewhere), maybe there should be a TypeScript ESLint warning for this.
TypeScript's private is soft private and has escape hatches like the ability to use bracket notation to access a private field.
The new # private field prefix is hard private and doesn't allow for this. In the example below, TS compiles private and # differently, and the console log at the bottom shows that accessing these values produces different results.
class Dog {
#barkAmount = 0;
personality = "happy";
constructor() {}
}
class Cat {
private meowAmount = 0;
personality = "angry";
constructor() {}
}
const fido = new Dog();
const garfield = new Cat();
console.log(
fido.#barkAmount, // no good
fido['#barkAmount'], // no good
garfield.meowAmount, // no good
garfield['meowAmount'] // fine
);
Compiles to:
"use strict";
class Dog {
constructor() {
this.#barkAmount = 0;
this.personality = "happy";
}
#barkAmount;
}
class Cat {
constructor() {
this.meowAmount = 0;
this.personality = "angry";
}
}
const fido = new Dog();
const garfield = new Cat();
console.log(fido.#barkAmount, fido['#barkAmount'], garfield.meowAmount, garfield['meowAmount']);
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der Durchsicht des TypeScript-Beispiels, das private und # gegenüberstellt, sowie des verlinkten TypeScript ESLint-Projekts. Kläre zunächst, ob das beabsichtigte Ergebnis Dokumentation, eine Compiler-Warnung oder eine ESLint-Warnung ist; als abgeschlossen gilt die Aufgabe, wenn ein abgestimmter Umfang und die entsprechende Dokumentation oder das entsprechende Diagnoseverhalten vorliegen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, typescript
- Bereich
- documentation, tooling
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 25/100