A template literal type lets you create new string types by combining other string literal types or string unions using backticks (`) and placeholders (${...}). It works like string templates but for types.
Click to reveal answer
beginner
How do you create a template literal type that combines two string literals, e.g., 'hello' and 'world'?
You write: `type Greeting = `${'hello'} ${'world'}`;` This creates a type that matches the string 'hello world'.
Click to reveal answer
intermediate
Can template literal types use unions inside placeholders? Give an example.
Yes. For example: `type Colors = 'red' | 'blue'; type Shades = `${Colors}Light` | `${Colors}Dark`;` This creates types like 'redLight', 'redDark', 'blueLight', and 'blueDark'.
Click to reveal answer
intermediate
What happens if you use a template literal type with a number or boolean type inside the placeholder?
TypeScript converts the number or boolean to a string literal type. For example, `type NumStr = `${1 | 2}`;` creates a type '1' | '2' as strings.
Click to reveal answer
beginner
Why are template literal types useful in TypeScript?
They help create flexible and precise string types by combining smaller types. This improves type safety and auto-completion for strings that follow patterns.
Click to reveal answer
Which syntax correctly defines a template literal type combining 'foo' and 'bar'?
Atype T = `${'foo'}${'bar'}`;
Btype T = 'foo' + 'bar';
Ctype T = 'foo' & 'bar';
Dtype T = 'foo' | 'bar';
✗ Incorrect
Template literal types use backticks and placeholders to combine strings, like `${'foo'}${'bar'}`.
What type does this produce? `type T = `${'a' | 'b'}${'1' | '2'}`;`
A'a' | 'b' | '1' | '2'
B'a1' | 'a2' | 'b1' | 'b2'
C'a12' | 'b12'
D'a' & '1' & 'b' & '2'
✗ Incorrect
It creates all combinations of the unions inside the template literal.
Can template literal types include expressions other than string literals?
AYes, but only boolean types
BNo, only string literals are allowed
CYes, including unions of strings and numbers converted to strings
DNo, only numbers are allowed
✗ Incorrect
Template literal types can include unions of strings, numbers, and booleans, which are converted to string literal types.
What is the result type of `type T = `${number}`;`?
Astring
Bnever
Cnumber
Dtemplate literal type representing any number as string
✗ Incorrect
It creates a template literal type representing string versions of numbers, but since number is broad, it acts like string.
Why use template literal types instead of plain string unions?
ATo build complex string patterns from smaller parts
BTo allow any string value
CTo convert numbers to booleans
DTo disable type checking
✗ Incorrect
Template literal types let you combine smaller string types into complex patterns for better type safety.
Explain what template literal types are and how they help in TypeScript.
Think about how string templates work in JavaScript but for types.
Confusing template literal types with runtime string concatenation
Thinking they define numeric ranges
Assuming they declare functions
2. Which of the following is the correct syntax to define a template literal type combining 'hello' and 'world' with a space in between?
easy
A. type Greeting = `${'hello'} ${'world'}`;
B. type Greeting = `hello world`;
C. type Greeting = `${'hello' + ' ' + 'world'}`;
D. type Greeting = '${hello} ${world}';
Solution
Step 1: Understand template literal syntax
Template literal types use backticks and ${} to embed string literals or types.
Step 2: Check each option
type Greeting = `${'hello'} ${'world'}`; correctly uses backticks and embeds 'hello' and 'world' with a space between them. type Greeting = `${'hello' + ' ' + 'world'}`; uses + inside template literal which is invalid. type Greeting = `hello world`; is a plain string literal, not combining types. type Greeting = '${hello} ${world}'; uses single quotes and incorrect syntax.
Final Answer:
type Greeting = `${'hello'} ${'world'}`; -> Option A
Quick Check:
Correct template literal syntax uses backticks and ${} [OK]
Hint: Use backticks and ${} to embed strings in template literal types [OK]
Common Mistakes:
Using + inside template literals
Using single quotes instead of backticks
Writing plain strings instead of template literal types
3. Given the following TypeScript code, what is the type of greeting?
type Hello = 'hello';
type World = 'world';
type Greeting = `${Hello} ${World}`;
const greeting: Greeting = 'hello world';
medium
A. `${Hello}${World}`
B. 'hello' | 'world'
C. string
D. 'hello world'
Solution
Step 1: Understand the template literal type definition
The type Greeting combines 'hello' and 'world' with a space, so it represents the exact string 'hello world'.
Step 2: Check the assigned value type
The variable greeting is assigned 'hello world', which matches the Greeting type exactly.
Final Answer:
'hello world' -> Option D
Quick Check:
Template literal type matches exact combined string [OK]
Hint: Template literal types produce exact combined string types [OK]
Common Mistakes:
Thinking the type is a union of 'hello' and 'world'
Assuming it is a generic string
Confusing the template literal syntax with a string literal
4. Identify the error in the following TypeScript code using template literal types:
type Prefix = 'pre';
type Suffix = 'fix';
type Combined = `${Prefix + Suffix}`;
const word: Combined = 'prefix';
medium
A. Missing backticks around the template literal
B. The variable word has wrong value assigned
C. Using + operator inside template literal types is invalid
D. Prefix and Suffix types must be numbers
Solution
Step 1: Check template literal syntax
Inside template literal types, you cannot use the + operator to concatenate types; you embed types directly with ${}.
Step 2: Identify the error
The expression `${Prefix + Suffix}` is invalid syntax. It should be `${Prefix}${Suffix}` without +.
Final Answer:
Using + operator inside template literal types is invalid -> Option C
Quick Check:
Template literal types concatenate by adjacent ${} without + [OK]
Hint: No + inside template literal types; just place ${} side by side [OK]
Common Mistakes:
Using + operator inside template literal types
Confusing runtime string concatenation with type syntax
Assuming types must be numbers to concatenate
5. You want to create a type EventName that represents strings like 'click_button' or 'hover_link' by combining event types 'click' | 'hover' and target types 'button' | 'link' separated by an underscore. Which of the following correctly defines EventName?
hard
A. type EventName = `${click | hover}_${button | link}`;
B. type EventName = `${'click' | 'hover'}_${'button' | 'link'}`;
C. type EventName = `${'click' & 'hover'}_${'button' & 'link'}`;
D. type EventName = '${click | hover}_${button | link}';
Solution
Step 1: Understand union types in template literals
Using union types inside ${} creates all combinations of the union members.
Step 2: Check syntax correctness
type EventName = `${'click' | 'hover'}_${'button' | 'link'}`; correctly uses backticks and unions inside ${} with an underscore between them. type EventName = '${click | hover}_${button | link}'; uses single quotes and invalid syntax. type EventName = `${'click' & 'hover'}_${'button' & 'link'}`; uses & which means intersection, not union, so it won't produce combinations. type EventName = `${click | hover}_${button | link}`; misses quotes around string literals.
Final Answer:
type EventName = `${'click' | 'hover'}_${'button' | 'link'}`; -> Option B
Quick Check:
Union types inside template literals create all string combinations [OK]
Hint: Use unions inside ${} with backticks to create all string combos [OK]
Common Mistakes:
Using & instead of | for unions
Omitting quotes around string literals
Using single quotes instead of backticks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.