Description
🔎 Search Terms
"instantiated with a different subtype of constraint", distributive, recursion, generic
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about distributive generics
⏯ Playground Link
💻 Code
enum Something {
Foo = 'foo',
Bar = 'bar',
}
export type DistributiveWithExtends<
T extends Something,
> = {
children:
T extends any
? ReadonlyArray<DistributiveWithExtends<T>>
: never;
};
export type DistributiveWithoutExtends<
T,
> = {
children:
T extends any
? ReadonlyArray<DistributiveWithoutExtends<T>>
: never;
};
export function withExtends<
T extends Something
>(
{ children }: DistributiveWithExtends<T>
) {
withExtends<T>(children[0]);
}
export function withoutExtends<
T
>(
{ children }: DistributiveWithoutExtends<T>
) {
withoutExtends<T>(children[0]);
}
🙁 Actual behavior
Argument of type 'DistributiveWithExtends<Something.Foo> | DistributiveWithExtends<Something.Bar>' is not assignable to parameter of type 'DistributiveWithExtends'.
Type 'DistributiveWithExtends<Something.Foo>' is not assignable to type 'DistributiveWithExtends'.
Type 'Something.Foo' is not assignable to type 'T'.
'Something.Foo' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'Something'.(2345)
🙂 Expected behavior
No type error. The distributed type ought to be assignable to the original non-distributed type.
Additional information about the issue
It looks like the relationship between the distributed type and the original type have been lost. The distributed type knows it extends Something
, but doesn't know that it extends T
.