1

I have an type

type mytype = 'tipe-1' | 'tipe-2' | 'tipe-3'

how to type like this type mytype2 = '1' | '2' | '3'

but the output still the same 'tipe-1' | 'tipe-2' | 'tipe-3'

1 Answers1

3

You can use Template Literal Types for this:

type MyType = `tipe-${1 | 2 | 3}`

This is identical to:

type MyType = 'tipe-1' | 'tipe-2' | 'tipe-3'

You can abstract this if you'd like to use it in multiple places:

type MakeMyType<T extends number> = `tipe-${T}`;

type MyType = MakeMyType<1 | 2 | 3>;
Dogbert
  • 212,659
  • 41
  • 396
  • 397