What if you could fix all your text case mistakes with just one simple type?
Why Capitalize and Uncapitalize types in Typescript? - Purpose & Use Cases
Imagine you have many words or labels in your program, and you need to change their first letter to uppercase or lowercase everywhere manually.
For example, turning 'apple' into 'Apple' or 'Banana' into 'banana' by hand in many places.
Doing this by hand is slow and easy to forget or make mistakes.
If you miss one place, your program might look inconsistent or even break.
Changing many words manually wastes time and causes frustration.
TypeScript's Capitalize and Uncapitalize types do this automatically for you.
They change the first letter of a string type to uppercase or lowercase without extra code.
This keeps your code clean, consistent, and error-free.
type Fruit = 'apple' | 'banana'; // Manually write 'Apple' | 'Banana' everywhere
type Fruit = 'apple' | 'banana'; type CapitalizedFruit = Capitalize<Fruit>; type UncapitalizedFruit = Uncapitalize<CapitalizedFruit>;
You can easily transform string types' first letters, making your code more flexible and readable.
When building a UI, you might want to display labels with the first letter capitalized automatically, no matter how the data comes in.
Manual letter changes are slow and error-prone.
Capitalize and Uncapitalize types automate first-letter changes.
This leads to cleaner, safer, and easier-to-maintain code.
Practice
Capitalize<T> utility type do in TypeScript?Solution
Step 1: Understand the purpose of Capitalize<T>
The Capitalize utility type changes only the first letter of a string type to uppercase.Step 2: Compare with other options
Other options describe different transformations like full uppercase or lowercase, which are not what Capitalize does.Final Answer:
It makes the first letter of a string type uppercase. -> Option DQuick Check:
Capitalize = first letter uppercase [OK]
- Thinking it changes the whole string to uppercase
- Confusing Capitalize with Uncapitalize
- Assuming it reverses the string
T in TypeScript?Solution
Step 1: Recall TypeScript generic syntax
Generic utility types use angle brackets <> to specify the type parameter, like Uncapitalize<T>.Step 2: Check case sensitivity and brackets
TypeScript is case sensitive and uses angle brackets, so 'Uncapitalize<T>' is correct, while parentheses or square brackets are invalid here.Final Answer:
Uncapitalize<T> -> Option AQuick Check:
Generic types use <> brackets [OK]
- Using parentheses instead of angle brackets
- Writing utility type names in lowercase
- Using square brackets instead of angle brackets
Capitalize<'hello world'> in TypeScript?Solution
Step 1: Apply Capitalize to the string literal
Capitalize changes only the first letter to uppercase, so 'hello world' becomes 'Hello world'.Step 2: Confirm the rest of the string remains unchanged
Only the first character changes; the rest stays lowercase as in the original string.Final Answer:
'Hello world' -> Option AQuick Check:
Capitalize<'hello world'> = 'Hello world' [OK]
- Making the entire string uppercase
- Not changing any letter
- Changing only the first letter to lowercase
type T = uncapitalize<'HelloWorld'>;
What is the error in this code?
Solution
Step 1: Check utility type name casing
TypeScript utility types like Uncapitalize must start with uppercase letters; 'uncapitalize' is invalid.Step 2: Verify other syntax elements
The string literal and angle brackets are correct; only the utility type name casing is wrong.Final Answer:
The utility type name should be capitalized as Uncapitalize. -> Option BQuick Check:
Utility types start with uppercase letters [OK]
- Using lowercase for utility type names
- Changing brackets to parentheses
- Thinking string literal case matters here
'apple' | 'banana' | 'cherry' using TypeScript's Capitalize utility?Solution
Step 1: Understand Capitalize on union types
Capitalizing a union type like 'apple' | 'banana' | 'cherry' directly does not capitalize each word separately.Step 2: Capitalize each string literal individually
Applying Capitalize to each string separately and then unioning them ensures each word's first letter is capitalized.Final Answer:
type Fruits = Capitalize<'apple'> | Capitalize<'banana'> | Capitalize<'cherry'>; -> Option CQuick Check:
Capitalize each union member separately [OK]
- Applying Capitalize to the whole union at once
- Using Uncapitalize instead of Capitalize
- Trying to capitalize a single string with all words
Ready to prove it?
Pass the test and this pattern moves unknown
