microsoft / microsoft/TypeScript

Recognize property with name defined by constant in Object.defineProperty

Offen
#38,115 1 Kommentar 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit zu überprüfen, wie der TypeScript-Compiler Aufrufe von Object.defineProperty und aus Konstanten oder Variablen abgeleitete Eigenschaftsnamen behandelt. Vergleiche das gewünschte Verhalten mit Issue #28694 sowie den JavaScript-Beispielen und Playground-Links; als erledigt gilt die Aufgabe, wenn erkannte Prototypeigenschaften die erwarteten Typinformationen erhalten, ohne dass sich das ausgegebene JavaScript ändert.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.