microsoft / microsoft/TypeScript
Account for discrepancy between soft privacy of `private` vs hard privacy of `#`
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
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']);
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Empieza revisando el ejemplo de TypeScript que contrasta private con # y el proyecto enlazado TypeScript ESLint. Primero determina si el resultado previsto es documentación, una advertencia del compilador o una advertencia de ESLint; se considerará terminado cuando haya un alcance acordado y la documentación o el comportamiento de diagnóstico correspondiente.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- javascript, typescript
- Área
- documentation, tooling
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Necesita aclaración
- Aptitud para principiantes
- 25/100