microsoft / microsoft/TypeScript

Distribute union types over generic function application

Ouverte
#52,295 5 commentaires 4 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

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

Description

Suggestion

🔍 Search Terms

generic union distribute function mapped

✅ 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

It should be possible to call a generic function with a union as input and separately resolve the generics for each member of the union. This mimics the way generic types can be distributed over a union.

📃 Motivating Example

Consider this simple example (playground):

type A<T> = (a: T) => T; // any type invariant on T
function foo<T>(a: A<T>) {}
declare const a: A<1> | A<2>;
foo(a); // error: A<1> | A<2> is not assignable to A<1 | 2>

The function foo is perfectly capable of handling an input of either A<1> or A<2>, but Typescript will not allow you to execute it on the union of those types. That is because it tries to find a single instantiation for T that works, but there is none, because the type A<T> is not covariant.

In the case that foo had an output, foo: <T>(a: A<T>) => B<T>, for input of A<1> | A<2> the output type would be B<1> | B<2>, much the same as how distributing over a union works in a type expression like X extends A<infer T> ? B<T> : never;

💻 Use Cases

This is one of a few issues that make non-covariant types a little bit second-class to work with in Typescript. And some of the other issues might be very hard to resolve, like how to type "An array of A<T> where each element can have a different T" without using any. But in comparison, I don't think this one requires any deep thought for the desired behavior, and while the implementation might be tricky I don't think it requires any truly new capabilities.

Workarounds:

  1. For a function where T doesn't appear in the output, like <T>(a: A<T>) => void, can be typed as (a: A<any>) => void. Then it works with unions as input. However, that introduces anys into the typechecking of the function's implementation, which don't need to be there. Instead, you can explicitly specify foo<any>(x) when calling the function. But if the function has other generics, that will make it so they also have to be explicitly specified instead of inferred.
  2. For a function where T does appear in the output, like <T>(a: A<T>) => B<T> where A<T> and B<T> are both invariant, I am not aware of any workaround except casting. Using any leaks into the output type and therefore the rest of your code. Even casting in that situation is more brittle than usual, as changes to the input union type or to the definition of the function's output type will both be lost.

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.

Piste de recherche

Commencez par reproduire l’exemple motivant dans le TypeScript Playground lié et lisez la discussion de l’issue sur l’inférence générique sur les unions. C’est terminé lorsqu’une fonction générique accepte A<1> | A<2>, résout le générique séparément pour chaque membre et produit la sortie d’union correspondante sans nécessiter any ni un cast.

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

Évaluation

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

Recevez les nouvelles issues par e-mail

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