Union and Intersection Types in TypeScript

In this quiz we will cover Union and Intersection Types in TypeScript

Last Updated :
Discuss
Comments

Question 1

What is a Union Type in TypeScript?

  • A type that can hold only string values

  • A type that allows a variable to have multiple possible types

  • A type that merges multiple types into one

  • A type that converts different types into a single type

Question 2

How do you define a union type in TypeScript?

  • By using a comma , between types

  • By using the | (pipe) operator between types

  • By using the & (ampersand) operator between types

  • By defining an array of types

Question 3

What is an Intersection Type in TypeScript?

  • A type that combines multiple types into one

  • A type that allows a variable to hold multiple values

  • A type that can only be assigned null or undefined

  • A type that converts values from one type to another

Question 4

Which operator is used to create Intersection Types in TypeScript?

  • | (pipe)

  • & (ampersand)

  • , (comma)

  • : (colon)

Question 5

What will be the type of value in the following code?

TypeScript
let value: string | number; 


  • Only string

  • Only number

  • Either string or number

  • A combination of string and number

Question 6

What happens when an object has an Intersection Type?

  • It must contain all properties from all the intersected types

  • It can contain properties from only one of the intersected types

  • It must have only common properties from all intersected types

  • It throws a TypeScript error

Question 7

What will be the output of the following code?

TypeScript
type A = { a: string }; type B = { b: number }; type C = A & B; const obj: C = { a: "Hello", b: 42 }; 
  • Compilation error

  • { a: "Hello", b: 42 }

  • { a: "Hello" }

  • { b: 42 }

Question 8

Which of the following is a correct use case for Union Types?

  • When a value can be of multiple types but only one at a time

  • When an object needs to include properties from multiple types

  • When merging arrays of different types

  • When creating a function with multiple return types

Question 9

How does TypeScript handle methods in Intersection Types if there are name conflicts?

  • It picks the first method definition

  • It picks the last method definition

  • It merges the methods into one

  • It results in a compilation error

Question 10

What is an advantage of using Union and Intersection Types?

  • They provide more flexibility in defining data structures

  • They eliminate the need for explicit type declarations

  • They improve runtime performance

  • They allow implicit type conversion

There are 10 questions to complete.

Take a part in the ongoing discussion