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 😀

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay