0
0
Typescriptprogramming~20 mins

Creating type aliases in Typescript - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Type Alias Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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");
AType error at compile time
BOperation succeeded\nOperation failed
Csuccess\nerror
DOperation failed\nOperation succeeded
Attempts:
2 left
💡 Hint
Check how the function uses the union type to decide what to print.
🧠 Conceptual
intermediate
1:30remaining
Understanding type alias usage
Which of the following best describes the purpose of a type alias in TypeScript?
AIt creates a new variable that holds a value of a specific type.
BIt imports types from external modules.
CIt declares a function that returns a specific type.
DIt defines a new name for an existing type or combination of types.
Attempts:
2 left
💡 Hint
Think about how type aliases help with readability and reuse.
🔧 Debug
advanced
2: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);
AError: Property 'y' is missing in type '{ x: number; }' but required in type 'Point'.
BNo error, outputs: { x: 10 }
CError: Type 'number' is not assignable to type 'string'.
DError: Cannot redeclare block-scoped variable 'p'.
Attempts:
2 left
💡 Hint
Check if all required properties of the type alias are present in the object.
📝 Syntax
advanced
1:30remaining
Correct syntax for type alias with intersection
Which option correctly defines a type alias combining two types using intersection?
Atype Combined = TypeA | TypeB;
Btype Combined = TypeA + TypeB;
Ctype Combined = TypeA & TypeB;
Dtype Combined = TypeA && TypeB;
Attempts:
2 left
💡 Hint
Intersection means the type must have all properties from both types.
🚀 Application
expert
2: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"]
};
AAn object with a status string and a payload array of strings.
BAn object with a status number and a payload string.
CAn array of strings.
DA string representing the status.
Attempts:
2 left
💡 Hint
Look at how the generic type parameter T is used in the alias.