0
0
Typescriptprogramming~20 mins

Template literal type syntax in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Template Literal Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Template Literal Type Concatenation
What is the type of variable result after this code runs?
Typescript
type Greeting = "Hello";
type Name = "Alice" | "Bob";
type Result = `${Greeting}, ${Name}!`;

let result: Result;
result = "Hello, Alice!";
A"Hello, Alice!"
B"Hello, Alice!" | "Hello, Bob!"
C"Hello, Alice!" | "Hello, Bob!" | "Hello, Charlie!"
Dstring
Attempts:
2 left
💡 Hint
Think about how template literal types combine unions.
Predict Output
intermediate
2:00remaining
Result of Nested Template Literal Types
What is the type of fullId after this code?
Typescript
type Prefix = "ID" | "UID";
type NumberStr = `${number}`;
type FullId = `${Prefix}-${NumberStr}`;

let fullId: FullId;
fullId = "UID-123";
A`${"ID" | "UID"}-${string}`
B"ID-123" | "UID-123"
Cstring
D"ID-" | "UID-"
Attempts:
2 left
💡 Hint
Remember that `${number}` becomes string but is still a template literal type.
🔧 Debug
advanced
2:00remaining
Identify the Error in Template Literal Type Usage
What error does this TypeScript code produce?
Typescript
type Color = "red" | "blue";
type Shade = "light" | "dark";
type ColoredShade = `${Shade} ${Color}`;

let paint: ColoredShade = "bright red";
AType '"bright red"' is not assignable to type 'ColoredShade'.
BSyntaxError: Unexpected token in template literal type.
CTypeError: Cannot use union types in template literals.
DNo error, code compiles successfully.
Attempts:
2 left
💡 Hint
Check if the assigned string matches the template literal type exactly.
🧠 Conceptual
advanced
2:00remaining
Understanding Template Literal Type Distributive Behavior
Given type A = "x" | "y" and type B = `${A}1`, what is the expanded type of B?
A"x" | "y" | "1"
B"x1"
C"x1" | "y1"
Dstring
Attempts:
2 left
💡 Hint
Template literal types distribute over unions.
Predict Output
expert
3:00remaining
Complex Template Literal Type with Conditional Types
What is the type of Result after this code?
Typescript
type Status = "success" | "error";
type Message<T extends Status> = T extends "success" ? `Operation was ${T}` : `Operation failed with ${T}`;
type Result = Message<Status>;
Astring
B"Operation was success"
C"Operation failed with error"
D"Operation was success" | "Operation failed with error"
Attempts:
2 left
💡 Hint
Conditional types distribute over unions.