0
0
Typescriptprogramming~5 mins

Capitalize and Uncapitalize types in Typescript

Choose your learning style9 modes available
Introduction

These types help change the first letter of a word to uppercase or lowercase. It makes text look neat or fit rules.

When you want to make the first letter of a string type uppercase.
When you want to make the first letter of a string type lowercase.
When you need to format type names or keys to follow naming rules.
When you want to clean up user input or data keys in types.
When you want to create new types based on existing ones but with changed first letter case.
Syntax
Typescript
Capitalize<S extends string>
Uncapitalize<S extends string>

Capitalize changes the first letter of the string type S to uppercase.

Uncapitalize changes the first letter of the string type S to lowercase.

Examples
This makes the first letter of 'hello' uppercase, so it becomes 'Hello'.
Typescript
type Upper = Capitalize<'hello'>;
// Result: 'Hello'
This makes the first letter of 'World' lowercase, so it becomes 'world'.
Typescript
type Lower = Uncapitalize<'World'>;
// Result: 'world'
Capitalizes the first letter of 'typescript'.
Typescript
type Mixed = Capitalize<'typescript'>;
// Result: 'Typescript'
Uncapitalizes the first letter of 'JavaScript'.
Typescript
type Mixed2 = Uncapitalize<'JavaScript'>;
// Result: 'javaScript'
Sample Program

This program shows how to use Capitalize and Uncapitalize types to change the first letter of string types. It then prints the results.

Typescript
type Capitalized = Capitalize<'apple'>;
type Uncapitalized = Uncapitalize<'Banana'>;

const fruit1: Capitalized = 'Apple';
const fruit2: Uncapitalized = 'banana';

console.log(fruit1);
console.log(fruit2);
OutputSuccess
Important Notes

These types only affect the first letter of the string type.

If the string is empty or the first character is not a letter, the result stays the same.

They work only on string literal types, not on general strings.

Summary

Capitalize makes the first letter uppercase.

Uncapitalize makes the first letter lowercase.

Use them to format string types easily in TypeScript.