DEV Community

Kaziu
Kaziu

Posted on • Edited on

define type of 'hoge_hoge_1' in typescript

I had a opportunity to define like this type hoge_hoge_1 which last character changes of number as string.

How to do

1. define hoge_hoge in the string.

this is easy, just add type of with template literal

${typeof 'hoge_hoge'}
Enter fullscreen mode Exit fullscreen mode

2. define number which change as string.

number can define like this

type Number = number
Enter fullscreen mode Exit fullscreen mode

in this time I need to define number as string, so just add template literal

type Number = `${number}`
Enter fullscreen mode Exit fullscreen mode

▼ This is final code

const PREFIX_ITEM_NAME = "hoge_hoge"

type NumericString = `${number}`
type HogeItemName =
  `${typeof PREFIX_ITEM_NAME}_${NumericString}`
Enter fullscreen mode Exit fullscreen mode

Problem

On my editor (webstorm), some errors appeared
Image description

I found that it is supported starting from TypeScript 4.1.
So I just fixed my setting of my editor and everything changed fine 😀

Top comments (0)