Skip to content

Cannot infer Template Literal Type containing ${number} / ${string} #43060

@kotarella1110

Description

@kotarella1110

Bug Report

Cannot infer Template Literal Type containing ${number} / ${string}

🔎 Search Terms

  • Template Literal Types number infer
  • Template Literal Types string infer

🕗 Version & Regression Information

  • This is the behavior of version 4.1 or higher.

⏯ Playground Link

Playground link with relevant code

💻 Code

// ${number} issue
type A<T> = T extends `${infer U}.${infer V}` ? U | V : never
type B = A<`test.1024`>; // This will be "test" | "1024" as expected
type C = A<`test.${number}`>; // Expected "test" | `${number}` or "test" | number but this will be never
type D<T> = T extends `${infer U}.${number}` ? U : never
type E = D<`test.1024`>; // This will be "test" as expected
type F = D<`test.${number}`>; // Expected "test" but this will be never
// ${string} issue
type G<T> = T extends `${infer U}.${infer V}` ? U | V : never
type H = G<`test.hoge`>; // This will be "test" | "hoge" as expected
type I = G<`test.${string}`>; // Expected "test" | `${string}` or "test" | string but this will be never
type J<T> = T extends `${infer U}.${string}` ? U : never
type K = J<`test.hoge`>; // This will be "test" as expected
type L = J<`test.${string}`>; // Expected "test" but this will be never

🙁 Actual behavior

  • C type is never.
  • F type is never.
  • I type is never.
  • L type is never.

🙂 Expected behavior

  • Expected C type to be "test" | `${number}` or "test" | number.
  • Expected F type to be "test".
  • Expected I type to be "test" | `${string}` or "test" | string.
  • Expected L type to be "test".

Metadata

Metadata

Assignees

Labels

FixedA PR has been merged for this issueSuggestionAn idea for TypeScript

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions