Closed rj-lee closed 1 week ago
(There are no distributive conditional types in this issue)
From previous issues having to do with branded primitives in template literals, it's not clear what the "correct" behavior should be here. Looks like different people have very different expectations.
I can see how people might want it differently, but what if we wanted to intentionally distribute it?
type Foo<T extends string> = T extends T ? `foo${T}` : never;
type FooN = Foo<`100` | `${number & {}}`>;
// FooN is still `foo${number & {}}`
I don't know any good way to reason about this. Either foo100
is a foo${number & {}}
, in which case reduction is correct, or it isn't, in which case foo${number & {}}
has no constituents.
Possibly if #33471 is ever implemented there would be a way to have this work also. The issue isnβt that the reduction is incorrect, but that it obscures information people want to keep around.
@RyanCavanaugh I assume this to be the template-string analogue of
type T = 1 | 2 | 3 | (number & {});
which is (IIUC, very much intentionally) not reduced today even though 1 | 2 | 3
are all subsumed by number & {}
This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes.
π Search Terms
template literal union distributive conditional type
π Version & Regression Information
5.4.5 and Nightly
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.4.5#code/C4TwDgpgBAYg9nAclAvFABgMwQEgN4DkAjAAwkFQA+UAFOvgHYCuAtgEYQBOAvulAGRQ83AJS8A3ACgA9NKihIsBMjRZceenmbsuvAUO68ZciAA9IAY2AQAJqigAibHFIkHVDM-ybtHHn0FhXiA
π» Code
π Actual behavior
'100'
and`${number}` & {}
are being simplified to`${number}` & {}
.FooN
does not have autocomplete for'foo100'
π Expected behavior
FooN
should be of type"foo100" | `foo${`${number}` & {}}
and provide autocomplete forfoo100
while allowingfoo101
or any other numberAdditional information about the issue
No response