Generics Aliasing and Partial Specialization

Offen
#45,419 4 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

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

Rechercherichtung

Es werden keine Quelldateien, Tests oder Einstiegspunkte genannt. Beginne mit der Durchsicht der Beispiele für Aliasing und partielle Spezialisierung sowie der TypeScript-Entwurfsziele; als abgeschlossen würde dies ein vereinbartes Design für generische Aliase und partielle Spezialisierung sowie eine Implementierung und Abdeckung für die angeforderten Fälle erfordern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

In Discussion Suggestion

Suggestion

🔍 Search Terms

List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.
generics specialization

✅ 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

Add Generics aliasing and partial Specialization

📃 Aliasing Example

// In external library

export type Monoid<T> = {
  concat: (x: T, y: T) => T;
  empty: T;
};
export type MonoidFn<T> = (concat: (x: T, y: T) => T, empty: T) => Monoid<T>;

// in user code
const monoid: MonoidFn<~T> = (concat, empty) => ({ concat, empty }); // impossible today, T is undefined

Here we want the monoid function to still be generic and still using the original library generic definition. Using ~T tells the compiler we know that T is undefined, just make an alias.

📃 Partial Specialization Example

// In external library

export type AggregateFn<T, U> =  (map: (item: T) => U) => (array: T[]) => U;

// in user code
const sum: AggregateFn<~T, number> = mapReduceSum<T,number>(); // impossible today, T is undefined

Here we want to specialize AggregateFn and make sum an AggregateFn<T, number>, that is still generic but specialized for numbers. Using ~T tells the compiler we know that T is undefined, so make a partial specialization.

💻 Use Cases

This is almost madatory for decent functional programming without having to redeclare functions of an external library.
Examples should be sufficient to explain a basic use case

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus microsoft/TypeScript

Alle Issues in microsoft/TypeScript

Ähnliche Issues

Weitere Issues zu Go

Neue Issues direkt in Ihr Postfach

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