-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Homomorphic mapped generic array type sometimes no longer seen as an array #57744
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
Comments
The constraint of |
Right, the core issue is that when a homomorphic mapped type is applied to an intersection of array types, the resulting type isn't an array type: type CheckArray<T extends any[]> = T;
type Mappy<T> = { [K in keyof T]: 1 };
type T0 = CheckArray<Mappy<[10, 20, 30]>>; // [1, 1, 1]
type T1 = CheckArray<Mappy<string[]>>; // 1[]
type T2 = CheckArray<Mappy<string[] & number[]>>; // Error The reason the error shows up in the OP is that with #57122 (which is in 5.4) we now recognize that the conditional type adds the constraint The fix here is probably to distribute the homomorphic mapped type over intersections, i.e. treat |
Yeah, homomorphic mapped types "should" distribute over intersections, but I guess I find it a little surprising that the combination of two array constraints just produces an intersection to begin with, since intersections of array types are so problematic. |
I would (perhaps naively) expect that |
Could someone triage this? Is it a bug/limitation/intended? If it's going to stick around like this for a while is there any workaround for people who run into it? Thanks! |
I'm considering the best way to fix this. The core issue is that mapped types applied to array intersections do not produce arrays, and that's is a design limitation. However, the error in the repro is a regression, which is unfortunate. The workaround is to remove the constraint from the type parameter and instead add it through an indirection: type MustBeArray<T extends any[]> = T
type Hmm2<T> = T extends number[] ?
MustBeArray<{ [I in keyof T]: 1 }> :
never;
type Hmm<T extends any[]> = Hmm2<T>;
type X = Hmm<[3, 4, 5]> |
Uh oh!
There was an error while loading. Please reload this page.
π Search Terms
conditional, mapped, array
π Version & Regression Information
β― Playground Link
Playground link
π» Code
π Actual behavior
{[I in keyof T]: 1}
is not seen as an array type inside ofHmm<T>
, causing a compiler error. Something about the conditional type re-constrainingT
to another array type causes it to fail.π Expected behavior
The code should compile without error.
Additional information about the issue
Comes from this Stack Overflow question.
I'm a bit concerned that I can't seem to work around this without
//@ts-ignore
, since the normal approaches ofExtract<T, any[]>
don't help (they end up just doing the same conditional type thing again which break things) andT & any[]
kind of works but you end up with intersections of arrays which nobody likes.The text was updated successfully, but these errors were encountered: