Challenge - 5 Problems
Template Literal Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Template Literal Type Concatenation
What is the type of
Result after this code runs?Typescript
type A = "Hello"; type B = "World"; type Result = `${A} ${B}!`; // What is Result?
Attempts:
2 left
💡 Hint
Template literal types combine string literal types exactly as written.
✗ Incorrect
The template literal type `${A} ${B}!` concatenates the literal types A and B with a space and exclamation mark, resulting in the exact string literal type "Hello World!".
❓ Predict Output
intermediate2:00remaining
Template Literal Type with Union Types
What is the type of
Greeting after this code?Typescript
type Name = "Alice" | "Bob"; type Greeting = `Hello, ${Name}!`; // What is Greeting?
Attempts:
2 left
💡 Hint
Template literal types distribute over unions.
✗ Incorrect
When a template literal type uses a union type inside, it creates a union of all possible combinations. Here, Greeting is the union of "Hello, Alice!" and "Hello, Bob!".
🔧 Debug
advanced2:00remaining
Why does this Template Literal Type cause an error?
This code causes a TypeScript error. What is the reason?
Typescript
type Prefix = "pre"; type Suffix = number; type Combined = `${Prefix}${Suffix}`; // Why is this an error?
Attempts:
2 left
💡 Hint
Check the allowed types inside template literal types.
✗ Incorrect
Template literal types accept string literal types or numeric literal types, but the type Suffix is the number type, not a string literal type or numeric literal type. It must be a string literal type or a numeric literal type.
🧠 Conceptual
advanced2:00remaining
Understanding Template Literal Type Inference
Given this function, what is the inferred return type of
makeKey('user', 42)?Typescript
function makeKey<T extends string | number, U extends string | number>(prefix: T, id: U) { return `${prefix}_${id}` as const; } const key = makeKey('user', 42); // What is the type of key?
Attempts:
2 left
💡 Hint
The function uses
as const to fix the return type.✗ Incorrect
The
as const assertion tells TypeScript to infer the exact string literal type of the returned template literal, so key has the type "user_42".❓ Predict Output
expert3:00remaining
Complex Template Literal Type with Conditional Types
What is the type of
Result after this code?Typescript
type Status = "success" | "error"; type Response<T extends Status> = T extends "success" ? `Response: ${T} with data` : `Response: ${T} with error`; // What is Response<"error">? type Result = Response<"error">;
Attempts:
2 left
💡 Hint
Conditional types choose the template based on the input type.
✗ Incorrect
For T = "error", the conditional type picks the false branch, so Result is the string literal type "Response: error with error".