DEV Community

Acid Coder
Acid Coder

Posted on • Edited on

Typescript WTF Moments 3: Type of Key is `string` if Key is Template Literal `a/${string}`

const a = 'a'
const obj = { [a]:1 }
type A = keyof typeof obj
//   ^?

const b:`b/${string}` = 'b/a'
const obj2 = { [b]:1 } 
type B = keyof typeof obj2
//   ^?

// expect type B to be `b/${string}` but it is not
Enter fullscreen mode Exit fullscreen mode

playground

this is a known widen string deficiency

it is part of this long going issue

Top comments (0)