Challenge - 5 Problems
Type Alias Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this TypeScript code using type aliases?
Consider the following TypeScript code that uses a type alias. What will be the output when this code runs?
Typescript
type ID = string | number; function printId(id: ID) { if (typeof id === 'string') { console.log(`ID as string: ${id.toUpperCase()}`); } else { console.log(`ID as number: ${id}`); } } printId('abc123'); printId(456);
Attempts:
2 left
💡 Hint
Think about how the function checks the type of the id and what methods are called on it.
✗ Incorrect
The type alias 'ID' allows the function to accept either a string or a number. The function checks the type and calls toUpperCase() only on strings, so the output shows the string in uppercase and the number as is.
🧠 Conceptual
intermediate1:30remaining
Why use type aliases instead of repeating types?
Why are type aliases useful in TypeScript when you have complex or repeated types?
Attempts:
2 left
💡 Hint
Think about how type aliases help with code clarity and reuse.
✗ Incorrect
Type aliases let you name complex types once and reuse them, which keeps code cleaner and easier to update.
🔧 Debug
advanced2:00remaining
What error does this TypeScript code raise?
This code uses a type alias but has a mistake. What error will TypeScript show?
Typescript
type User = {
name: string;
age: number;
};
const user: User = {
name: 'Alice',
age: '25'
};Attempts:
2 left
💡 Hint
Check the type of the 'age' property in the object compared to the alias.
✗ Incorrect
The 'age' property is given a string value '25', but the type alias requires a number, so TypeScript reports a type mismatch error.
🚀 Application
advanced2:00remaining
How does a type alias improve this function's parameter clarity?
Given this function signature without a type alias, how would using a type alias improve clarity and maintenance?
Typescript
function sendMessage(user: { id: number; name: string }, message: string) { console.log(`${user.name} says: ${message}`); }
Attempts:
2 left
💡 Hint
Think about how naming a type helps when the same structure is used multiple times.
✗ Incorrect
Using a type alias for the user object lets you write the type once and reuse it, making the function signature simpler and the code easier to maintain.
❓ Predict Output
expert2:30remaining
What is the output of this TypeScript code with nested type aliases?
Analyze the code below using nested type aliases. What will be printed to the console?
Typescript
type Coordinates = { x: number; y: number };
type Point = Coordinates & { label: string };
const point: Point = { x: 10, y: 20, label: 'A' };
console.log(`${point.label}: (${point.x}, ${point.y})`);Attempts:
2 left
💡 Hint
Look at how the type aliases combine properties and how the object is created.
✗ Incorrect
The type alias 'Point' combines 'Coordinates' and adds 'label'. The object matches this type, so the console prints the label followed by coordinates.