DEV Community

Cover image for How to convert the first letter of a string literal type into a lowercase format or uncapitalize in TypeScript?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

How to convert the first letter of a string literal type into a lowercase format or uncapitalize in TypeScript?

Originally posted here!

To convert the first letter of string literal type into a lowercase format or uncapitalize, you can use the built-in Uncapitalize type that comes with TypeScript.

TL;DR

// string literal type
type Greeting = "HELLO WORLD";

// convert first letter of
// string literal type
// into lowercase format or uncapitalize
type GreetingUncapitalized = Uncapitalize<Greeting>; // hELLO WORLD
Enter fullscreen mode Exit fullscreen mode

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

// string literal type
type Greeting = "HELLO WORLD";
Enter fullscreen mode Exit fullscreen mode

Now to uncapitalize the string literal type let's use the Uncapitalize 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 = "HELLO WORLD";

// convert first letter of
// string literal type
// into lowercase format or uncapitalize
type GreetingUncapitalized = Uncapitalize<Greeting>; // hELLO WORLD
Enter fullscreen mode Exit fullscreen mode

Now if you hover over the GreetingUncapitalized type you can see that the HELLO WORLD string literal type is now uncapitalized. Yay 🥳.

See the above code live in codesandbox.

That's all 😃!

Feel free to share if you found this useful 😃.


Top comments (0)