DEV Community

Cover image for How to convert a string literal type into an uppercase format in TypeScript?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

How to convert a string literal type into an uppercase format in TypeScript?

Originally posted here!

To convert a string literal type into an uppercase format, you can use the built-in Uppercase type that comes with TypeScript.

TL;DR

// lowercase string literal type
type Greeting = "hello";

// convert string literal type
// into uppercase format
type GreetingUppercase = Uppercase<Greeting>; // HELLO
Enter fullscreen mode Exit fullscreen mode

For example, let's say we have a string literal type that is in the lowercase format like this,

// lowercase string literal type
type Greeting = "hello";
Enter fullscreen mode Exit fullscreen mode

Now to convert the string literal type into uppercase let's use the Uppercase built-in type and pass the Greeting type into it using the angled bracket (<>) syntax.

It can be done like this,

// lowercase string literal type
type Greeting = "hello";

// convert string literal type
// into uppercase format
type GreetingUppercase = Uppercase<Greeting>; // HELLO
Enter fullscreen mode Exit fullscreen mode

Now if you hover over the GreetingUppercase type you can see that the hello string literal type is now converted into an uppercase format. Yay 🥳.

See the above code live in codesandbox.

That's all 😃!

Feel free to share if you found this useful 😃.


Top comments (0)