Template literal types let you create new string types by combining other string types. This helps catch mistakes and make your code clearer.
Template literal types in Typescript
type NewType = `${Prefix}${Middle}${Suffix}`;You use backticks (`) around the template, like in JavaScript template strings.
Each part inside ${} must be a string literal type or a union of string literals.
type Greeting = `Hello, ${"World" | "Friend"}!`;type EventName = `${"on" | "off"}${"Click" | "Hover"}`;type CssClass = `btn-${"primary" | "secondary" | "danger"}`;This program defines a type Status with three possible values. Then it creates a template literal type Message that combines a fixed string with Status. The function showMessage only accepts strings matching the Message type. Trying to pass a wrong string causes an error.
type Status = "success" | "error" | "loading"; type Message = `Status is: ${Status}`; function showMessage(msg: Message) { console.log(msg); } showMessage("Status is: success"); // showMessage("Status is: done"); // Error: Argument of type '"Status is: done"' is not assignable to parameter of type 'Message'.
Template literal types only work with string literal types, not general strings.
They help catch errors early by limiting allowed string values.
You can combine them with unions to create many possible string patterns.
Template literal types build new string types by combining smaller string types.
They help TypeScript check if strings match specific patterns.
Use them to make your code safer and clearer when working with strings.
Practice
template literal types in TypeScript?Solution
Step 1: Understand template literal types
Template literal types allow combining string literal types to form new string types.Step 2: Differentiate from runtime string operations
They are used at compile time for type checking, not for runtime string concatenation or numeric ranges.Final Answer:
To create new string types by combining existing string types -> Option AQuick Check:
Template literal types build new string types [OK]
- Confusing template literal types with runtime string concatenation
- Thinking they define numeric ranges
- Assuming they declare functions
'hello' and 'world' with a space in between?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 AQuick Check:
Correct template literal syntax uses backticks and ${} [OK]
- Using + inside template literals
- Using single quotes instead of backticks
- Writing plain strings instead of template literal types
greeting?
type Hello = 'hello';
type World = 'world';
type Greeting = `${Hello} ${World}`;
const greeting: Greeting = '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 DQuick Check:
Template literal type matches exact combined string [OK]
- 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
type Prefix = 'pre';
type Suffix = 'fix';
type Combined = `${Prefix + Suffix}`;
const word: Combined = 'prefix';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 CQuick Check:
Template literal types concatenate by adjacent ${} without + [OK]
- Using + operator inside template literal types
- Confusing runtime string concatenation with type syntax
- Assuming types must be numbers to concatenate
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?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 BQuick Check:
Union types inside template literals create all string combinations [OK]
- Using & instead of | for unions
- Omitting quotes around string literals
- Using single quotes instead of backticks
Ready to prove it?
Pass the test and this pattern moves unknown
