Suggestion: ClassMethodDecoratorContext's name property could pick up Value["name"]
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 25/100
- Issue-Typ
- Bug
- Klarheit
- Größtenteils klar
- Aktivitätsstatus
- Veraltet
- Tech-Stack
- javascript, typescript
- Bereich
- compilers
Rechercherichtung
Beginne mit der Definition von ClassMethodDecoratorContext und vergleiche den vorgeschlagenen bedingten Namenstyp mit der ECMAScript Function.name-Spezifikation. Verwende das verlinkte Playground, um die abgeleiteten Ergebnisse für die benannten und unbenannten Fälle zu überprüfen. Als abgeschlossen gilt die Aufgabe, wenn geklärt ist, ob die vorgeschlagene lib-definition-Änderung gemäß der Spezifikation gültig ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
lib Update Request
Configuration Check
My compilation target is ESNext and my lib is the default.
Missing / Incorrect Definition
interface ClassMethodDecoratorContext<
This = unknown,
Value extends (this: This, ...args: any) => any = (this: This, ...args: any) => any,
> {
// ...
/** The name of the decorated class element. */
readonly name: string | symbol;
// ...
We could change the name field to: readonly name: Value extends { name: string } ? Value["name"] : (string | symbol);. Most of the time, Value["name"] is just string, so this would be a non-breaking change (if slightly less intuitive).
However, if I were to augment my method type definitions to have a name property, TypeScript could pick up on it:
Sample Code
export type NumberStringType = {
repeatForward(s: string, n: number): string;
repeatBack(n: number, s: string): string;
};
type NST_RF_raw = NumberStringType["repeatForward"]["name"]
// ^? type NST_RF_raw = string
type MethodsOnlyType = {
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
[key: string | number | symbol]: (...args: any[]) => any
}
type NamedMethodsOnlyType<T extends MethodsOnlyType> = {
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
[Key in keyof T]: T[Key] & { name: Key }
}
type NST_RF_named = NamedMethodsOnlyType<NumberStringType>["repeatForward"]["name"];
// ^? type NST_RF_named = "repeatForward"
type DefaultInferredName = ClassMethodDecoratorContext<
// ^? type DefaultInferredName = string | symbol
NumberStringType, NamedMethodsOnlyType<NumberStringType>["repeatForward"]
>["name"];
interface ClassMethodDecoratorContext_Named<
This = unknown,
Value extends (this: This, ...args: any) => any = (this: This, ...args: any) => any,
> {
readonly name: Value extends { name: string } ? Value["name"] : (string | symbol);
}
type DirectName = ClassMethodDecoratorContext_Named<
// ^? type Directname = "repeatForward"
NumberStringType, NamedMethodsOnlyType<NumberStringType>["repeatForward"]
>["name"];
type InferredName = ClassMethodDecoratorContext_Named<
// ^? type InferredName = string
NumberStringType, NumberStringType["repeatForward"]
>["name"];
Motivation
I've been experimenting with method decorators for about a week now, to implement some aspect-oriented programming (or at least, my understanding of it). One facet I haven't yet cracked is removing the need for the method name on a decorator:
class NST_Class extends NumberStringClass {
@precondition<"repeatForward">(callForwardPre)
repeatForward(s: string, n: number): string {
return super.repeatForward(s, n);
}
}
Since the context has a name field of string | symbol type, I can't infer the name from that... I think.
I am perfectly willing to be wrong and have this whole ticket closed as invalid!
Documentation Link
The ECMAScript 2023 specification for Function.name says I'm possibly wrong here: it specifies the name field must be a string (specifically, "not a symbol"). This could be just a specification bug with the introduction of symbol keys, but I am not ready to assert that at this time.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beitragsleitfaden
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus microsoft/TypeScript
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
microsoft/TypeScript#64322 · 2 Kommentare · 1 Reaktion · 2 zugewiesene Personen ·
-
Possible Improvement
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
microsoft/TypeScript#64278 · 1 Kommentar · 1 Reaktion ·
-
Docs
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
microsoft/TypeScript#64118 · 1 Kommentar ·
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 88/100
microsoft/TypeScript#64094 ·
-
Docs
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
microsoft/TypeScript#63959 · 5 Kommentare ·
Alle Issues in microsoft/TypeScript
Ähnliche Issues
-
optimization optimization:agents-md-curator
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 86/100
githubnext/gh-aw-cao#13143 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 84/100
blinklabs-io/bursa#904 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 84/100
yanet-platform/ipfw-go#129 ·
-
bug confmap/provider/googlesecretmanagerprovider needs triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
open-telemetry/opentelemetry-collector-contrib#51273 · 2 Kommentare ·
-
bug: AI Gateway client filter lists "Unknown" twice when NULL and literal Unknown clients coexist Offenbug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 90/100