microsoft / microsoft/TypeScript

Recognize property with name defined by constant in Object.defineProperty

Aperta
#38,115 1 commento 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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

Playground Link

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia esaminando come il compilatore TypeScript gestisce le chiamate a Object.defineProperty e i nomi delle proprietà derivati da costanti o variabili. Confronta il comportamento richiesto con l’issue #28694 e con gli esempi JavaScript e i link al playground; il lavoro è completato quando le proprietà del prototipo riconosciute ricevono le informazioni sui tipi previste senza modificare il JavaScript emesso.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.