Skip to content

Union in template literal simplifying unexpectedlyΒ #58340

Description

@rj-lee

πŸ”Ž 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

type FooN = `foo${'100' | (`${number}` & {})}`;
// type FooN = `foo${`${number}` & {}}`
// expected = "foo100" | `foo${`${number}` & {}}`

πŸ™ 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 for foo100 while allowing foo101 or any other number

Additional information about the issue

No response

Activity

  1. jcalz commented on Apr 27, 2024

    @jcalz
    Contributor

    (There are no distributive conditional types in this issue)

    #48034 #48044 #49839 #54188 #54648 #56458 #57807 #57918

    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.

  2. rj-lee commented on Apr 27, 2024

    @rj-lee
    Author

    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 & {}}`
  3. RyanCavanaugh commented on Apr 29, 2024

    @RyanCavanaugh
    Member

    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.

  4. jcalz commented on Apr 29, 2024

    @jcalz
    Contributor

    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.

  5. fatcerberus commented on Apr 30, 2024

    @fatcerberus

    Ryan Cavanaugh (@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 & {}

  6. typescript-bot commented on May 3, 2024

    @typescript-bot
    Contributor

    This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  7. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions