DEV Community

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

Posted on • Originally published at melvingeorge.me

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

Originally posted here!

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

TL;DR

// string literal type
type Greeting = "Hi, How Are you?";

// convert string literal type
// into lowercase format
type GreetingLowercase = Lowercase<Greeting>; // hi, how are you?
Enter fullscreen mode Exit fullscreen mode

For example, let's say we have a string literal type like this,

// string literal type
type Greeting = "Hi, How Are you?";
Enter fullscreen mode Exit fullscreen mode

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

It can be done like this,

// string literal type
type Greeting = "Hi, How Are you?";

// convert string literal type
// into lowercase format
type GreetingLowercase = Lowercase<Greeting>; // hi, how are you?
Enter fullscreen mode Exit fullscreen mode

Now if you hover over the GreetingLowercase type you can see that the Hi, How Are you? string literal type is now converted into a lowercase format. Yay 🥳.

See the above code live in codesandbox.

That's all 😃!

Feel free to share if you found this useful 😃.


Top comments (0)