Implement `Capitalize<T>` which converts the first letter of a string to uppercase and leave the rest as-is. Master TypeScript template literal types in this medium-level challenge on TypeScriptPro.
In this medium-level challenge, you'll implement Capitalize<T> which converts the first letter of a string to uppercase and leave the rest as-is.
Implement Capitalize<T> which converts the first letter of a string to uppercase and leave the rest as-is.
For example
type capitalized = Capitalize<'hello world'> // expected to be 'Hello world'Change the following code to make the test cases pass (no type check errors).
type MyCapitalize<S extends string> = S extends `${infer First}${infer Rest}`
? `${Uppercase<First>}${Rest}`
: SHow it works:
S to be a string with extends stringS extends ${infer First}${infer Rest} matches the string type and captures the first letter into First and the rest of the string into Rest? ${Uppercase<First>}${Rest} returns the string with the first letter capitalized: S returns the string if it doesn't start with a letterThis challenge helps you understand TypeScript's template literal types and how to apply this concept in real-world scenarios.
This challenge is originally from here.
Be the first to access the course, unlock exclusive launch bonuses, and get special early-bird pricing before anyone else.
Only 27 Spots left
Get 1 month early access
Pre-Launch discount