microsoft / microsoft/TypeScript
Generics are lost during `Function.prototype.bind()` and `Function.prototype.call()`
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
Suggestion
🔍 Search Terms
generic, bind, call
✅ Viability 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, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
⭐ Suggestion
Currently, binding or calling a function loses its generics. Suggestion: Don't lose the generic.
📃 Motivating Example
Let's say I have a generic function getMediaAd
export async function getMediaAd<T extends Ad>(
this: Client,
params: { locale?: string; media_id: string; format: T['format'] },
) {
const url = this.cmsUrl(`api/ads/random`, params)
if (!params.locale) url.searchParams.set('locale', this.locale)
return this.get<T>(url, this.cmsHeaders())
}
This function can obtain multiple types of ad formats, determined by T.
I want to put this function inside a Client class to access some client-specific features.
export class Client {
getMediaAd = getMediaAd.bind(this)
}
However, when I do that, this.getMediaAd loses its generic parameter.
Losing the generic parameter means I can't tell it what is T supposed to be, like this:
export async function getMediaBillboardAd(
this: Client,
params: { locale?: Intl.UnicodeBCP47LocaleIdentifier; media_id: string },
) {
return this.getMediaAd<AdBillboard>({ ...params, format: adFormat.billboard })
}
Here, T is AdBillboard. However, getMediaAd expects 0 generic parameters.
💻 Use Cases
I want to use this to create an API wrapper client, but keep the ability to declare functions in different files.
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 el ejemplo genérico motivador de getMediaAd y la asignación de Client.getMediaAd mediante Function.prototype.bind(). Compara el comportamiento indicado de bind() y call() con el sistema de tipos actual de TypeScript y, después, define cómo deben conservarse los parámetros genéricos sin cambiar el comportamiento en tiempo de ejecución de JavaScript.
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
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100