microsoft / microsoft/TypeScript

Generic decorators - could they receive some default type arguments?

Offen
#2,607 18 Kommentare 54 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Domain: Decorators In Discussion Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

A decorator can receive arguments, by being defined as a function that accepts parameters and then returns a normal decorator function:

function computed(evaluator: (obj: any) => any) {
    return (obj: any, key: string) => {
        Object.defineProperty(obj, key, { 
            get() { return evaluator(obj); }
        });
    };
}

I could use the above example like this:

class C {
    firstName = "Homer";
    lastName = "Simpson";

    @computed(c => c.firstName + " " + c.lastName) 
    fullName: string;
}

Not a realistic example in that it's just another way of defining a property getter, but it's enough to demonstrates the general problem: c is of type any so the access to firstName and lastName is not type-checked, and nor is the result of the expression.

We can attempt to address this manually, because decorators can be generic:

function computed<O, R>(evaluator: (obj: O) => R) {
    return (obj: O, key: string) => {
        Object.defineProperty(obj, key, { 
            get() { return evaluator(obj); }
        });
    };
}

class C {
    firstName = "Homer";
    lastName = "Simpson";

    @computed<C, string>(c => c.firstName + " " + c.lastName)
    fullName: string;
}

But this still doesn't close the loop. The compiler is happy with the following abuse, in which c is supposedly a string but is actually a C, and fullName is declared a string but will actually become a number (albeit NaN I guess):

@computed<string, number>(c => parseInt(c, 10))
fullName: string;

In summary: decorators are passed the values of the meta-information in standard parameters (target, key, value), but they are not passed the compile-time types of those values in a way that can be used to create fully type-safe decorators.

So it would be helpful if in decorator-defining functions with generic parameters, those parameters could somehow map automatically to the types of target and value (as given meaning in the __decorate helper).

For big yucks, a straw man in which the compiler recognises a couple of built-in decorators that can appear on the type parameters:

function computed<@decoratorTarget O, @decoratorValue R>(evaluator: (obj: O) => R) {
    ...
}

Being decorated in this way, it would not be possible to manually specify type arguments for those type parameters when using the decorator. They would effectively be invisible to the user. If the decorator has its own custom type parameters, they would appear from the outside to be the only parameters (and for clarity should be at the front of the list).

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

Das Issue enthält Beispiele für Decorators und generische Typargumente, nennt aber keine Repository-Dateien, Tests oder Einstiegspunkte. Beginne damit, die Compilerbereiche zu finden, die Decorators typprüfen und generische Argumente inferieren, und definiere anschließend das unterstützte Verhalten sowie die erforderlichen Tests, um die typsichere Inferenz von Ziel und Wert zu verifizieren.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

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