microsoft / microsoft/TypeScript

Generics are lost during `Function.prototype.bind()` and `Function.prototype.call()`

Offen
#54,707 4 Kommentare 13 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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.

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

Beginne mit dem motivierenden generischen getMediaAd-Beispiel und der Zuweisung von Client.getMediaAd mithilfe von Function.prototype.bind(). Vergleiche das beschriebene Verhalten von bind() und call() mit dem aktuellen TypeScript-Typsystem und lege anschließend fest, wie generische Parameter erhalten bleiben sollen, ohne das Laufzeitverhalten von JavaScript zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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