Skip to content

Distributive generic with extends constraint not assignable to original non-distributed type #61866

Open
@Benjamin-Dobell

Description

@Benjamin-Dobell

🔎 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

https://www.typescriptlang.org/play/?ts=5.8.3#code/KYOwrgtgBAyg9hYAXAFgSxAcygbwFBRQBiccUAvFAOQBmpVANAVAEICGAThdQEaeN4AvnjzAAHgAc4HJFCQBPCcCgARNAGckHNDzBI0AN2AB1NKgCiYpKAAm6gDzMAKlHHWQd2AmTosTAHzc+IQAxugANjYcoABczIQubrbqUGwg8vGEUAD8UABKwGw2cCDh8gCCHBxs8vZqmtq6+kamFlbJ9k7+-pmEMVAgwEYcANxCY6KS0rIKSqoaWjp6hiZmKHB6lu52jgkBQcxhaJHRIHFZUIntHilpGReEuQVFJWWV1bX1i00rreub1x2XR6Dyg-UGwzGggm4ikMigNDAIBC+hKUAA7msth1nK5ASl4IhUBhMHh-AAKZg4KBHE6gKCCfpfRrLFpY-GdHoASkIwQx7O2Di65NpUVAAG0AAwAXS5UJEsOmCKRKLQaMxqA2SGxN12lzJlN5NIiYpADKZCxZzVWmoBgs5eB5uGYGv+2o5wtFpylsvlQA

💻 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions