microsoft / microsoft/TypeScript
Suggestion: ClassMethodDecoratorContext's name property could pick up Value["name"]
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
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.
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 con la definición de ClassMethodDecoratorContext y compara el tipo de nombre condicional propuesto con la especificación de ECMAScript Function.name. Usa el Playground enlazado para verificar los resultados inferidos para los casos con nombre y sin nombre. Se considera terminado cuando se resuelva si el cambio propuesto en lib-definition es válido según la especificación.
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
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 25/100