📖 Scenario: You are building a simple greeting system that creates personalized messages based on a person's name and the time of day.
🎯 Goal: Create a TypeScript program that uses template literal types combined with union types to generate greeting messages.
📋 What You'll Learn
Use union types for names and times of day
Use a template literal type to combine the unions into greeting messages
Create a variable with the combined greeting type
Print the greeting message
💡 Why This Matters
🌍 Real World
Template literal types with unions help create precise string types for things like personalized messages, URLs, or CSS class names.
💼 Career
Understanding template literal types is useful for TypeScript developers to write safer and more expressive code, especially in UI and API development.
Progress0 / 4 steps
1
Create union types for names and times of day
Create a union type called Name with the values 'Alice' and 'Bob'. Then create a union type called TimeOfDay with the values 'morning' and 'evening'.
Typescript
Hint
Use the pipe symbol | to create union types in TypeScript.
2
Create a template literal type for greetings
Create a template literal type called Greeting that combines Name and TimeOfDay into strings like 'Good morning, Alice!' or 'Good evening, Bob!'.
Typescript
Hint
Use backticks ` and ${} to create template literal types.
3
Create a variable with a greeting message
Create a variable called message of type Greeting and assign it the value 'Good morning, Alice!'.
Typescript
Hint
Declare the variable with const and specify the type Greeting.
4
Print the greeting message
Use console.log to print the variable message.
Typescript
Hint
Use console.log(message); to print the message to the console.
Practice
(1/5)
1.
Which of the following best describes a template literal with unions in TypeScript?
easy
A. A method to concatenate numbers and strings without type checking.
B. A way to create specific string patterns by combining fixed text and union types.
C. A function that returns a template string without any type restrictions.
D. A way to declare variables without specifying their types.
Solution
Step 1: Understand template literals and unions
Template literals allow embedding expressions in strings. Union types restrict values to a set of options.
Step 2: Combine both concepts
Template literal with unions creates strings with fixed parts and limited variable parts, improving type safety.
Final Answer:
A way to create specific string patterns by combining fixed text and union types. -> Option B
Quick Check:
Template literal + union = specific string patterns [OK]
Hint: Think of fixed text plus limited choices in strings [OK]
Common Mistakes:
Confusing unions with simple string concatenation
Ignoring type safety benefits
Thinking it's only about functions
2.
Which of the following TypeScript declarations correctly uses a template literal with unions?
type Size = "small" | "medium" | "large";
type Message = `Size is ${Size}`;
easy
A. type Message = `Size is ${string}`;
B. type Message = `Size is ${boolean}`;
C. type Message = `Size is ${number}`;
D. type Message = `Size is ${Size}`;
Solution
Step 1: Check the union type used inside template literal
The union type Size is "small" | "medium" | "large" and should be used inside the template literal.
Step 2: Match the template literal syntax
The correct syntax is `Size is ${Size}` to restrict the string to those sizes.
Final Answer:
type Message = `Size is ${Size}`; -> Option D
Quick Check:
Use union type inside template literal exactly [OK]
Hint: Use the union type name inside ${} exactly [OK]
Common Mistakes:
Replacing union with general types like string or number
Using incorrect syntax for template literals
Forgetting backticks around the template
3.
What is the type of result in the following code?
type Color = "red" | "green" | "blue";
type Alert = `Alert: ${Color}`;
const result: Alert = "Alert: green";
medium
A. "Alert: red" | "Alert: green" | "Alert: blue"
B. "Alert: red" | "Alert: green"
C. "Alert: green"
D. string
Solution
Step 1: Understand the template literal with union
The type Alert creates strings by combining "Alert: " with each value in Color.
Step 2: Expand the union to all possible strings
So Alert is the union of "Alert: red", "Alert: green", and "Alert: blue".