Bird
Raised Fist0
Typescriptprogramming~20 mins

Creating type aliases in Typescript - Practice Exercises

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of creating a type alias in TypeScript?
easy
A. To import external modules
B. To create a new variable
C. To define a function
D. To give a new name to a type for easier reuse

Solution

  1. Step 1: Understand what a type alias does

    A type alias creates a new name for an existing type, making code easier to read and reuse.
  2. Step 2: Compare options with the definition

    Only To give a new name to a type for easier reuse correctly describes this purpose; others describe unrelated concepts.
  3. Final Answer:

    To give a new name to a type for easier reuse -> Option D
  4. Quick Check:

    Type alias = new name for type [OK]
Hint: Type aliases rename types to simplify code [OK]
Common Mistakes:
  • Confusing type alias with variable declaration
  • Thinking type alias creates new data
  • Mixing type alias with function definitions
2. Which of the following is the correct syntax to create a type alias named Age for the type number?
easy
A. alias Age: number;
B. type Age = number;
C. let Age: number;
D. type Age == number;

Solution

  1. Step 1: Recall TypeScript syntax for type aliases

    The correct syntax uses the keyword type, followed by the alias name, an equals sign, and the type.
  2. Step 2: Check each option

    type Age = number; matches the correct syntax exactly. Options B, C, and D have syntax errors or wrong keywords.
  3. Final Answer:

    type Age = number; -> Option B
  4. Quick Check:

    type alias syntax = type Name = Type [OK]
Hint: Use 'type Name = Type;' to create aliases [OK]
Common Mistakes:
  • Using 'alias' instead of 'type'
  • Using '==' instead of '='
  • Confusing type alias with variable declaration
3. What will be the output of this TypeScript code?
type ID = string | number;
function printId(id: ID) {
  console.log(typeof id);
}
printId(123);
printId('abc');
medium
A. Error at compile time
B. string number
C. number string
D. ID ID

Solution

  1. Step 1: Understand the type alias and function behavior

    The alias ID allows string or number. The function prints the type of the argument.
  2. Step 2: Trace the function calls

    Calling printId(123) prints 'number'. Calling printId('abc') prints 'string'.
  3. Final Answer:

    number string -> Option C
  4. Quick Check:

    typeof 123 = number, typeof 'abc' = string [OK]
Hint: Check typeof values for union type inputs [OK]
Common Mistakes:
  • Assuming type alias changes runtime behavior
  • Expecting compile error for union types
  • Mixing order of printed types
4. Identify the error in this TypeScript code:
type User = {
  name: string;
  age: number;
}

const user: User = {
  name: 'Alice',
  age: '25'
};
medium
A. Property 'age' should be a number, not a string
B. Missing semicolon after type alias
C. Type alias name cannot be 'User'
D. Object properties must be declared with 'let'

Solution

  1. Step 1: Check the type alias definition

    The alias User expects age to be a number.
  2. Step 2: Check the object assigned to user

    The object has age: '25', which is a string, causing a type error.
  3. Final Answer:

    Property 'age' should be a number, not a string -> Option A
  4. Quick Check:

    Type mismatch in object property [OK]
Hint: Match property types exactly in objects [OK]
Common Mistakes:
  • Ignoring type mismatch in object properties
  • Thinking semicolon is mandatory after type alias
  • Confusing variable declaration rules with type alias
5. Given these type aliases:
type Point = { x: number; y: number };
type Circle = { center: Point; radius: number };
type Shape = Circle | Point;

Which of the following is a valid Shape object?
hard
A. { center: { x: 0, y: 0 }, radius: 5 }
B. { x: 0, y: 0, radius: 5 }
C. { center: { x: 0 }, radius: 5 }
D. { x: '0', y: 0 }

Solution

  1. Step 1: Understand the type aliases

    Point is an object with x and y as numbers. Circle has a center of type Point and a radius number. Shape can be either.
  2. Step 2: Check each option against the types

    { center: { x: 0, y: 0 }, radius: 5 } matches Circle exactly. { x: 0, y: 0, radius: 5 } has extra radius on a Point shape, invalid. { center: { x: 0 }, radius: 5 } has incomplete center. { x: '0', y: 0 } has x as string, invalid.
  3. Final Answer:

    { center: { x: 0, y: 0 }, radius: 5 } -> Option A
  4. Quick Check:

    Valid Shape = Circle or Point matching types [OK]
Hint: Match object shape exactly for union types [OK]
Common Mistakes:
  • Mixing properties of union types incorrectly
  • Using wrong property types inside objects
  • Assuming partial objects are valid
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard