microsoft / microsoft/TypeScript

Support declaring multiple setter overloads

Abierto
#60,664 2 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Awaiting More Feedback Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

🔍 Search Terms

setter overload, multiple setter signatures, multiple setter input types

✅ Viability Checklist
⭐ Suggestion

TypeScript allows declaring multiple different argument signatures for a function, i.e. "function overloads": https://www.typescriptlang.org/docs/handbook/2/functions.html#function-overloads.

We should be able to do the same for property setters, since they are modeled much like a function taking a single argument.

(Note that unlike some other languages, TypeScript's overloads are only bare type signatures: there is only a single method body implementation shared by all of them, making this a pure typechecking feature with no impact on the generated runtime code. This proposal works the same way, just extending this syntax from functions/methods to setters as well).

📃 Motivating Example

A setter might want to separate different types of inputs for improved clarity of documentation:

/**
 * Set startTime to a specific timestamp, specified as a Date object or number of ms since epoch.
 */
set startTime(date: Date | number);

/**
 * Set startTime to the start of the most recent activity matching the given category name.
 */
set startTime(category: string);

set startTime(dateOrCategory: Date | number | string) {
    // ...
}

Note that these differ not just in documentation but also in the value argument's name, which often appears in generated docs output too.

💻 Use Cases

Although setter overloads are necessarily less versatile than function overloads with multiple parameters, some of the same rationales for the overload feature still apply to setters – as seen in the example above.

Workaround
As with functions before overloading is supported, the workaround is just to glom all the docs together with some additional verbiage, e.g.:

/**
 * Set startTime:
 * - If given a Date object or number of ms, sets to a specific timestamp.
 * - If given a category name string, sets to the start of the most recent activity matching that name.
 */
set startTime(dateOrCategory: Date | number | string) {
    // ...
}

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Revisa la sintaxis propuesta para las sobrecargas de setter y el ejemplo motivador del issue. Determina cómo se deben comprobar los tipos de varias firmas de setter manteniendo una sola implementación y una salida de JavaScript sin cambios; se considera terminado cuando el feature está soportado sin cambiar el comportamiento existente en tiempo de ejecución.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Bien especificado
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.