Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript map string literal types to uppercase

type a = 'one' | 'two'

I would like to have a type b like

type b = 'ONE' | 'TWO'

So I tried

type a = 'one' | 'two'
type b = {[P in a]: P['toUpperCase']}

But that does not do what I want it to do.

Thank you for reading :)

like image 800
Max Coplan Avatar asked Jun 25 '20 18:06

Max Coplan


1 Answers

You can now do this in the coming 4.1 release, with the introduction of Template Literal Types:

type A = 'one' | 'two'
type B = `${Uppercase<A>}`

let b: B = 'one' // Type '"one"' is not assignable to type '"ONE" | "TWO"'.

TS Playground

like image 117
Derek Nguyen Avatar answered Oct 14 '22 06:10

Derek Nguyen