Template literal type syntax in Typescript - Time & Space Complexity
We want to understand how the time it takes to check types grows when using template literal types in TypeScript.
Specifically, how does the complexity change as the template parts get longer or more complex?
Analyze the time complexity of the following code snippet.
type EventName = `on${string}`;
function handleEvent(event: E) {
// handle event
}
// Example usage:
handleEvent('onclick');
handleEvent('onhover');
This code defines a template literal type that matches strings starting with "on" followed by any text, then uses it to type a function parameter.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: TypeScript compiler checks if the input string matches the template literal pattern.
- How many times: This check happens once per usage, but internally the compiler may traverse the string parts to verify the pattern.
As the string length or complexity of the template parts grows, the compiler does more work to match the pattern.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows roughly in direct proportion to the input size.
Time Complexity: O(n)
This means the time to check the template literal type grows linearly with the length of the string.
[X] Wrong: "The template literal type check is instant no matter the string length."
[OK] Correct: The compiler must examine each character or segment to confirm the pattern, so longer strings take more time.
Understanding how type checks scale helps you write efficient types and anticipate compiler behavior, a useful skill in real projects and interviews.
"What if we changed the template literal to include multiple variable parts? How would the time complexity change?"
Practice
Solution
Step 1: Understand template literal types
Template literal types use backticks and ${} to combine string types into new string types.Step 2: Compare options
Only Creates new string types by combining other string types using backticks and ${}. correctly describes this behavior; others describe unrelated concepts.Final Answer:
Creates new string types by combining other string types using backticks and ${}. -> Option BQuick Check:
Template literal types combine strings = A [OK]
- Confusing template literal types with string variables
- Thinking template literals create functions
- Assuming they create number types
Solution
Step 1: Identify correct template literal syntax
Template literal types use backticks (`) around the string with ${} for type insertion.Step 2: Check each option
Only `type Greeting = `${string} world`;` uses backticks correctly; the others use single/double quotes or miss backticks.Final Answer:
type Greeting = `${string} world`; -> Option DQuick Check:
Backticks + ${} = A [OK]
- Using single or double quotes instead of backticks
- Omitting backticks entirely
- Placing ${} outside backticks
type Name = "Alice" | "Bob";
type Greeting = `Hello, ${Name}!`;
let greet1: Greeting = "Hello, Alice!";
let greet2: Greeting = "Hello, Charlie!";Which line causes a TypeScript error?
Solution
Step 1: Understand the Greeting type
Greeting is a template literal type combining "Hello, " with Name ("Alice" or "Bob") and "!".Step 2: Check assigned values
"Hello, Alice!" matches Greeting, but "Hello, Charlie!" does not because "Charlie" is not in Name.Final Answer:
let greet2: Greeting = "Hello, Charlie!"; -> Option AQuick Check:
Invalid string not in union = B [OK]
- Assuming any string fits template literal type
- Ignoring union type restrictions
- Confusing variable assignment with type definition
type Status = "success" | "error";
type Message = `${Status}` | number;
let msg: Message = 404;Solution
Step 1: Analyze the type Message
Message is a union of a template literal type `${Status}` (which is "success" or "error") and number.Step 2: Check variable assignment
Assigning 404 (a number) to msg is valid because Message includes number.Final Answer:
There is no error; the code is valid. -> Option AQuick Check:
Union allows number assignment = D [OK]
- Thinking template literals can't be unioned with other types
- Assuming syntax error in template literal usage
- Believing number assignment is invalid here
Solution
Step 1: Understand the goal
Create a type for specific margin sides using template literal types with a union of side strings.Step 2: Evaluate options
type MarginProperty = `margin-${"top" | "right" | "bottom" | "left"}`; uses template literal with union of exact sides, producing only valid margin properties. type MarginProperty = "margin-top" | "margin-right" | "margin-bottom" | "margin-left"; works but is not using template literals. type MarginProperty = `margin-${string}`; allows any string after "margin-", which is too broad. type MarginProperty = `margin-${number}`; uses number, which is invalid here.Final Answer:
type MarginProperty = `margin-${"top" | "right" | "bottom" | "left"}`; -> Option CQuick Check:
Template literal + union for sides = C [OK]
- Using string instead of union inside template literal
- Listing all options without template literals
- Using number instead of string union
Ready to prove it?
Pass the test and this pattern moves unknown
