Generics Aliasing and Partial Specialization

Ouverte
#45,419 4 commentaires 1 réaction 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Évaluation

Difficulté
5/5
Temps estimé
Plus d'une semaine
Accessibilité débutants
20/100
Type d'issue
Fonctionnalité
Clarté
Plutôt claire
Activité
À l'abandon
Stack technique
typescript
Domaine
compilers

Piste de recherche

Aucun fichier source, test ou point d’entrée n’est nommé. Commencez par examiner les exemples d’aliasing et de spécialisation partielle ainsi que les objectifs de conception de TypeScript ; la tâche ne serait considérée comme terminée qu’avec une conception convenue pour les alias génériques et la spécialisation partielle, ainsi qu’une implémentation et une couverture des cas demandés.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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

Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de microsoft/TypeScript

Toutes les issues de microsoft/TypeScript

Issues similaires

Plus d'issues Go

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.