Challenge - 5 Problems
Type Alias Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a type alias with union types
What is the output of the following TypeScript code when compiled and run with
console.log?Typescript
type Status = "success" | "error"; function printStatus(status: Status) { if (status === "success") { console.log("Operation succeeded"); } else { console.log("Operation failed"); } } printStatus("success"); printStatus("error");
Attempts:
2 left
💡 Hint
Check how the function uses the union type to decide what to print.
✗ Incorrect
The type alias 'Status' allows only 'success' or 'error'. The function prints a message based on the value. Calling with 'success' prints 'Operation succeeded', and with 'error' prints 'Operation failed'.
🧠 Conceptual
intermediate1:30remaining
Understanding type alias usage
Which of the following best describes the purpose of a type alias in TypeScript?
Attempts:
2 left
💡 Hint
Think about how type aliases help with readability and reuse.
✗ Incorrect
Type aliases let you create a new name for an existing type or a combination of types, making code easier to read and maintain.
🔧 Debug
advanced2:00remaining
Identify the error in type alias usage
What error will this TypeScript code produce?
Typescript
type Point = { x: number; y: number };
const p: Point = { x: 10 };
console.log(p);Attempts:
2 left
💡 Hint
Check if all required properties of the type alias are present in the object.
✗ Incorrect
The type alias 'Point' requires both 'x' and 'y' properties. The object assigned to 'p' only has 'x', so TypeScript reports a missing property error.
📝 Syntax
advanced1:30remaining
Correct syntax for type alias with intersection
Which option correctly defines a type alias combining two types using intersection?
Attempts:
2 left
💡 Hint
Intersection means the type must have all properties from both types.
✗ Incorrect
The '&' symbol is used in TypeScript to create an intersection type alias combining all properties from both types.
🚀 Application
expert2:30remaining
Determine the type of a complex alias
Given the following type alias, what is the type of variable
data?Typescript
type Response<T> = {
status: "ok" | "error";
payload: T;
};
const data: Response<string[]> = {
status: "ok",
payload: ["apple", "banana"]
};Attempts:
2 left
💡 Hint
Look at how the generic type parameter T is used in the alias.
✗ Incorrect
The type alias 'Response' creates an object with a 'status' property that can be 'ok' or 'error', and a 'payload' property of type T. Here, T is string[], so payload is an array of strings.