-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Distribute union types over generic function application #52295
Comments
I'd be extremely worried about this happening in practice. Unions regularly contain hundreds of members, and resolving a generic function call is not a cheap operation. Worse, if a union appears in the output, then this is very prone to combinatorial explosion. A codebase full of calls like this could take minutes or hours to check (or just run out of memory). |
@RyanCavanaugh That makes sense. Though I'd have thought type operators using As for performance, it might not have to be implemented as actually doing a separate resolution for each member of the union. Currently, if you call |
You can distribute the union explicitly as a workaround: type A<T> = (a: T) => T; // any type invariant on T
function foo<T>(a: T extends unknown ? A<T> : never) {
}
declare const a: A<1> | A<2>;
foo(a); // ok |
Unfortunately this doesn't work if the parameters are objects: type AR = { a: string };
type BR = { b: string };
type A<T> = (a: T) => T; // any type invariant on T
function foo<T>(a: T extends unknown ? A<T> : never) {}
declare const a: A<AR> | A<BR>;
foo(a); // error |
This is preventing me from instantiating dynamic components based on my configuration file in my angular project. For example, i have a list of components that my client wants:
defined in my environment file for that client.
this code gives error without the |
Suggestion
π Search Terms
generic union distribute function mapped
β Viability Checklist
My suggestion meets these guidelines:
β 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):
The function
foo
is perfectly capable of handling an input of eitherA<1>
orA<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 typeA<T>
is not covariant.In the case that
foo
had an output,foo: <T>(a: A<T>) => B<T>
, for input ofA<1> | A<2>
the output type would beB<1> | B<2>
, much the same as how distributing over a union works in a type expression likeX 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 differentT
" without usingany
. 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:
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 introducesany
s into the typechecking of the function's implementation, which don't need to be there. Instead, you can explicitly specifyfoo<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.T
does appear in the output, like<T>(a: A<T>) => B<T>
whereA<T>
andB<T>
are both invariant, I am not aware of any workaround except casting. Usingany
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.The text was updated successfully, but these errors were encountered: