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)

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay