microsoft / microsoft/TypeScript
Generics Aliasing and Partial Specialization
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Non vengono indicati file sorgente, test o punti di ingresso. Inizia esaminando gli esempi di aliasing e specializzazione parziale e gli obiettivi di progettazione di TypeScript; per considerare il lavoro completo, sarebbe necessario concordare un design per gli alias generici e la specializzazione parziale, oltre a implementare e coprire i casi richiesti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 20/100