-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot infer Template Literal Type containing ${number}
/ ${string}
#43060
Comments
I'm sure this goes without saying, but the issue here is not specific to |
${number}
${number}
/ ${string}
I just came across this issue when trying to type a REST API. I made a type using templates that could successfully determine the appropriate return type of For my purposes, I would be happy if there were some way to tell TypeScript to interpret a template string as a string literal. E.g., Some confusion I have with the current implementation is that type type of a When doing const x = 4.5;
const y = `${x}abc` as const
const x:number = 4.5;
const y = `${x}abc` as const will not match against anything more specific than Perhaps tagged-template syntax could be used to specify a template as abstract or weakly inferred? For example type A<T> = T extends `${infer U}.${infer V}` ? U | V : never
type B<T> = T extends weak`${infer U}.${infer V}` ? U | V : never
const x = 4.5
const y:number = 4.5
type X = A<`${typeof x}.abc`> // "4" | "5.abc"
type Y = A<`${typeof y}.abc`> // never
type WeakX = B<`${typeof x}.abc`> // 4.5 | "abc" OR `${4.5}` | "abc"
type WeakY = B<`${typeof y}.abc`> // number | "abc" OR `${number}` | "abc"
type WeakXMovedDecimal = B<`${typeof x}a.bc`> // "4.5a" | "bc" OR `${4.5}a` | "bc"
type WeakYMovedDecimal = B<`${typeof y}a.bc`> // string | "bc" OR `${number}a` | "bc"
const z1:`${typeof x}abc` = `${x}abc` // Okay
const z2:`${typeof y}abc` = `${y}abc` // Fails typechecking
const z3:weak`${typeof x}abc` = `${x}abc` // Okay
const z4:weak`${typeof y}abc` = `${y}abc` // Okay I'm not sure if these rules would be consistent, but the basic idea would be to not expand |
Bug Report
Cannot infer Template Literal Type containing
${number}
/${string}
🔎 Search Terms
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
C
type isnever
.F
type isnever
.I
type isnever
.L
type isnever
.🙂 Expected behavior
C
type to be"test" | `${number}`
or"test" | number
.F
type to be"test"
.I
type to be"test" | `${string}`
or"test" | string
.L
type to be"test"
.The text was updated successfully, but these errors were encountered: