Capitalize and Uncapitalize types in Typescript - Time & Space Complexity
We want to understand how the time needed to run code changes when using Capitalize and Uncapitalize types in TypeScript.
Specifically, how does the work grow as the input string gets longer?
Analyze the time complexity of the following code snippet.
type Capitalized = Capitalize<"hello">;
type Uncapitalized = Uncapitalize<"World">;
// Using template literal types to capitalize first letter
type CapitalizeString<S extends string> = S extends `${infer F}${infer R}` ? `${Uppercase<F>}${R}` : S;
// Using template literal types to uncapitalize first letter
type UncapitalizeString<S extends string> = S extends `${infer F}${infer R}` ? `${Lowercase<F>}${R}` : S;
This code defines types that change the first letter of a string to uppercase or lowercase.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: TypeScript checks the first character of the string and changes its case.
- How many times: Only once per string type, no loops or recursion over the whole string.
The operation only looks at the first letter, so the time does not grow with the string length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The work stays the same no matter how long the string is.
Time Complexity: O(1)
This means the time to capitalize or uncapitalize is constant and does not depend on the string length.
[X] Wrong: "Changing the case of a string type takes longer for longer strings because it processes every character."
[OK] Correct: The type only changes the first character and leaves the rest unchanged, so it does not process the whole string.
Understanding how TypeScript handles string manipulation types helps you reason about type-level programming and its efficiency, a useful skill in advanced TypeScript coding.
"What if we changed the type to capitalize every letter in the string? How would the time complexity change?"
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
