Bird
Raised Fist0
Typescriptprogramming~5 mins

Type alias for unions in Typescript - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is a type alias in TypeScript?
A type alias gives a new name to a type. It helps make code easier to read and reuse by naming complex types.
Click to reveal answer
beginner
How do you create a union type alias in TypeScript?
Use the type keyword followed by a name, then an equals sign, and list types separated by |. For example: type MyUnion = string | number;
Click to reveal answer
beginner
What does the union type string | number mean?
It means a value can be either a string or a number. The value must match one of the types in the union.
Click to reveal answer
intermediate
Why use a type alias for unions instead of writing the union everywhere?
It makes code cleaner and easier to update. If the union changes, you only update the alias once instead of many places.
Click to reveal answer
intermediate
Can type aliases be used with complex unions like objects and primitives?
Yes! You can create aliases for unions of any types, including objects, primitives, or even other type aliases.
Click to reveal answer
Which syntax correctly defines a type alias for a union of string and number?
Atype MyType = string & number;
Balias MyType = string & number;
Calias MyType = string | number;
Dtype MyType = string | number;
What does the union type alias type Status = 'success' | 'error' | 'loading'; represent?
AA value that can be any string
BA value that must be exactly 'success', 'error', or 'loading'
CA value that can be a number or string
DA value that is always a boolean
Why might you use a type alias for a union type?
ATo simplify code and make it easier to update
BTo make the code harder to read
CTo repeat the same union everywhere
DTo avoid using unions
Which of these is NOT a valid union type alias?
Atype Data = string & number;
Btype Option = boolean | null;
Ctype Status = 'open' | 'closed';
Dtype Result = string | number;
Can a type alias for a union include other type aliases?
AOnly if they are primitive types
BNo, type aliases cannot be nested
CYes, type aliases can be combined in unions
DOnly if they are interfaces
Explain what a type alias for a union is and why it is useful in TypeScript.
Think about naming a group of types together.
You got /3 concepts.
    Write a simple example of a type alias for a union of string literals representing colors.
    Use single quotes for string literals.
    You got /3 concepts.

      Practice

      (1/5)
      1.

      What does a type alias for unions in TypeScript do?

      easy
      A. Defines a function that returns different types
      B. Creates a new variable with multiple values
      C. Gives a single name to multiple possible types
      D. Declares a constant with union values

      Solution

      1. Step 1: Understand the purpose of type alias

        A type alias lets you give a name to a type or group of types for easier use.
      2. Step 2: Understand union types

        A union type means a value can be one of several types, like string or number.
      3. Final Answer:

        Gives a single name to multiple possible types -> Option C
      4. Quick Check:

        Type alias for unions = single name for multiple types [OK]
      Hint: Type alias names multiple types combined with | [OK]
      Common Mistakes:
      • Thinking it creates variables instead of naming types
      • Confusing union with function return types
      • Assuming it declares constants
      2.

      Which of the following is the correct syntax to create a type alias MyType that can be either string or number?

      easy
      A. type MyType = string | number;
      B. alias MyType = string & number;
      C. type MyType = string & number;
      D. alias MyType = string | number;

      Solution

      1. Step 1: Recall correct keyword for type alias

        Use the keyword type to create a type alias, not alias.
      2. Step 2: Use union operator for multiple types

        The union operator is |, not &. So string | number is correct.
      3. Final Answer:

        type MyType = string | number; -> Option A
      4. Quick Check:

        type + union (|) = correct syntax [OK]
      Hint: Use 'type' keyword and '|' for union types [OK]
      Common Mistakes:
      • Using 'alias' instead of 'type'
      • Using '&' instead of '|' for unions
      • Mixing syntax from other languages
      3.

      What will be the output of the following TypeScript code?

      type Status = "success" | "error";
      
      function printStatus(s: Status) {
        console.log(s);
      }
      
      printStatus("success");
      printStatus("error");
      // printStatus("pending"); // Uncommenting this line causes error
      medium
      A. No output
      B. success\nerror\npending
      C. Error at runtime
      D. success\nerror

      Solution

      1. Step 1: Understand the type alias Status

        Status can only be "success" or "error" strings.
      2. Step 2: Check function calls

        Calling printStatus with "success" and "error" prints those values. "pending" is invalid and commented out.
      3. Final Answer:

        success\nerror -> Option D
      4. Quick Check:

        Valid union values print correctly [OK]
      Hint: Only allowed union values print without error [OK]
      Common Mistakes:
      • Assuming invalid union values print without error
      • Thinking it causes runtime error
      • Ignoring commented invalid call
      4.

      Identify the error in this TypeScript code using a type alias for unions:

      type ID = number | string;
      
      let userId: ID = true;
      medium
      A. Type 'boolean' is not assignable to type 'number | string'
      B. Missing semicolon after type alias
      C. Type alias cannot use union types
      D. Variable 'userId' must be declared with 'const'

      Solution

      1. Step 1: Check type alias definition

        ID allows only number or string values.
      2. Step 2: Check assigned value type

        Assigning true is boolean, which is not allowed by the union.
      3. Final Answer:

        Type 'boolean' is not assignable to type 'number | string' -> Option A
      4. Quick Check:

        Assigning wrong type causes error [OK]
      Hint: Assigned value must match union types exactly [OK]
      Common Mistakes:
      • Thinking union allows any type
      • Ignoring type mismatch errors
      • Confusing syntax errors with type errors
      5.

      Given the type alias type Response = { status: "ok"; data: string } | { status: "error"; error: string };, which of the following objects matches the Response type?

      hard
      A. { status: "ok", error: "File missing" }
      B. { status: "ok", data: "File loaded" }
      C. { status: "error", data: "File loaded" }
      D. { status: "error", error: 404 }

      Solution

      1. Step 1: Analyze the first union type

        When status is "ok", object must have a string property named data.
      2. Step 2: Analyze the second union type

        When status is "error", object must have a string property named error.
      3. Step 3: Check each option

        { status: "ok", data: "File loaded" } matches first type correctly. Options B and C mix properties incorrectly. { status: "error", error: 404 } has error property as number, not string.
      4. Final Answer:

        { status: "ok", data: "File loaded" } -> Option B
      5. Quick Check:

        Match status with correct property name and type [OK]
      Hint: Match status value with correct property name and type [OK]
      Common Mistakes:
      • Mixing properties from different union types
      • Using wrong property types
      • Ignoring the status-property relationship
      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