microsoft / microsoft/TypeScript
Recognize property with name defined by constant in Object.defineProperty
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
Search Terms
defineProperty
https://github.com/microsoft/TypeScript/issues/28694 is touching same domain
Suggestion
(Note: I'm using TS in JS files, have basically no knowledge about "pure" TS)
Compiler should recognize prototype property added using Object.defineProperty with name defined not only by string constant but also a string variable, e.g.:
const propName = 'myProp';
Object.defineProperty(Test.prototype, propName, { get: () => 'myVal' });
Playground Link
(included also desired behavior for decorators, but as it's experimental feature, you won't propably even consider it)
I understand it's not always possible to make sure variable with property name is not reassigned during runtime, but it seems that TS compiler can handle detection of "static" variables that have known value during compilation.
Use Cases
This way we could define class properties using "single source" and not duplicating code. For example a class providing access to key-value pairs object in a dotted notation instead of string values for keys, whilst the object can be used for another purpose.
Examples
// i can use these somewhere else, e.g. as a type
export const defaults = {
one: 1,
two: 2
}
// properties added in defineProperty
export class Test {
// i don't want to repeat myself
one = defaults.one;
/** @param {keyof defaults} key */
get(key) {
return defaults[key];
}
}
Object.keys(defaults).forEach((key) =>
Object.defineProperty(Test.prototype, key, { get: () => defaults[key] })
);
const inst = new Test();
inst.one; // who would not like this?
inst.get('one'); // instead of this
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.
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
Comienza revisando cómo el compilador de TypeScript maneja las llamadas a Object.defineProperty y los nombres de propiedades derivados de constantes o variables. Compara el comportamiento solicitado con el issue #28694 y con los ejemplos de JavaScript y los enlaces al playground; se considera terminado cuando las propiedades de prototipo reconocidas reciben la información de tipos esperada sin cambiar el JavaScript emitido.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- javascript, typescript
- Área
- compilers
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100