Bird
Raised Fist0
Typescriptprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does a template literal type in TypeScript do?
easy
A. Defines a function that returns a string.
B. Creates new string types by combining other string types using backticks and ${}.
C. Declares a variable with a fixed string value.
D. Creates a number type from string literals.

Solution

  1. Step 1: Understand template literal types

    Template literal types use backticks and ${} to combine string types into new string types.
  2. 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.
  3. Final Answer:

    Creates new string types by combining other string types using backticks and ${}. -> Option B
  4. Quick Check:

    Template literal types combine strings = A [OK]
Hint: Remember: template literal types build new string types with `${}` [OK]
Common Mistakes:
  • Confusing template literal types with string variables
  • Thinking template literals create functions
  • Assuming they create number types
2. Which of the following is the correct syntax for a template literal type in TypeScript?
easy
A. type Greeting = ${string} world;
B. type Greeting = '${string} world';
C. type Greeting = "${string} world";
D. type Greeting = `${string} world`;

Solution

  1. Step 1: Identify correct template literal syntax

    Template literal types use backticks (`) around the string with ${} for type insertion.
  2. Step 2: Check each option

    Only `type Greeting = `${string} world`;` uses backticks correctly; the others use single/double quotes or miss backticks.
  3. Final Answer:

    type Greeting = `${string} world`; -> Option D
  4. Quick Check:

    Backticks + ${} = A [OK]
Hint: Use backticks ``, not quotes, for template literal types [OK]
Common Mistakes:
  • Using single or double quotes instead of backticks
  • Omitting backticks entirely
  • Placing ${} outside backticks
3. Given the code:
type Name = "Alice" | "Bob";
type Greeting = `Hello, ${Name}!`;

let greet1: Greeting = "Hello, Alice!";
let greet2: Greeting = "Hello, Charlie!";

Which line causes a TypeScript error?
medium
A. let greet2: Greeting = "Hello, Charlie!";
B. let greet1: Greeting = "Hello, Alice!";
C. Both lines cause errors
D. Neither line causes an error

Solution

  1. Step 1: Understand the Greeting type

    Greeting is a template literal type combining "Hello, " with Name ("Alice" or "Bob") and "!".
  2. Step 2: Check assigned values

    "Hello, Alice!" matches Greeting, but "Hello, Charlie!" does not because "Charlie" is not in Name.
  3. Final Answer:

    let greet2: Greeting = "Hello, Charlie!"; -> Option A
  4. Quick Check:

    Invalid string not in union = B [OK]
Hint: Check if inserted string matches union type exactly [OK]
Common Mistakes:
  • Assuming any string fits template literal type
  • Ignoring union type restrictions
  • Confusing variable assignment with type definition
4. Identify the error in this TypeScript code using template literal types:
type Status = "success" | "error";
type Message = `${Status}` | number;

let msg: Message = 404;
medium
A. There is no error; the code is valid.
B. The template literal type syntax is incorrect; it needs backticks.
C. The variable msg cannot be assigned a number if Message includes number.
D. Template literal types cannot be combined with number types using |.

Solution

  1. Step 1: Analyze the type Message

    Message is a union of a template literal type `${Status}` (which is "success" or "error") and number.
  2. Step 2: Check variable assignment

    Assigning 404 (a number) to msg is valid because Message includes number.
  3. Final Answer:

    There is no error; the code is valid. -> Option A
  4. Quick Check:

    Union allows number assignment = D [OK]
Hint: Union types allow multiple types including template literals and numbers [OK]
Common Mistakes:
  • Thinking template literals can't be unioned with other types
  • Assuming syntax error in template literal usage
  • Believing number assignment is invalid here
5. How can you create a type that represents all possible CSS margin properties like "margin-top", "margin-right", "margin-bottom", and "margin-left" using template literal types?
hard
A. type MarginProperty = `margin-${string}`;
B. type MarginProperty = "margin-top" | "margin-right" | "margin-bottom" | "margin-left";
C. type MarginProperty = `margin-${"top" | "right" | "bottom" | "left"}`;
D. type MarginProperty = `margin-${number}`;

Solution

  1. Step 1: Understand the goal

    Create a type for specific margin sides using template literal types with a union of side strings.
  2. 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.
  3. Final Answer:

    type MarginProperty = `margin-${"top" | "right" | "bottom" | "left"}`; -> Option C
  4. Quick Check:

    Template literal + union for sides = C [OK]
Hint: Use union inside ${} to limit template literal options [OK]
Common Mistakes:
  • Using string instead of union inside template literal
  • Listing all options without template literals
  • Using number instead of string union
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard