Mastering advanced interfaces and types in TypeScript helps developers build robust, scalable, and flexible applications. This set of 30 MCQs covers topics such as extending interfaces, intersection and union of interfaces, mapped and conditional types, and utility types like Partial, Readonly, Pick, and Record.
Advanced Interfaces and Types in TypeScript – MCQs
1. Extending Interfaces
Which keyword is used to extend an interface in TypeScript?
a) extends
b) include
c) inherit
d) implements
Can an interface extend multiple interfaces in TypeScript?
a) No, interfaces can only extend one interface
b) Yes, interfaces can extend multiple interfaces
c) Yes, but only classes can extend multiple interfaces
d) No, only classes can extend multiple interfaces
What happens when an interface extends multiple interfaces with conflicting properties?
a) TypeScript throws a compilation error
b) The property from the first interface is used
c) The property from the last interface is used
d) TypeScript merges the properties and allows the conflict
Which of the following is a correct way to extend multiple interfaces? interface A { propA: string; } interface B { propB: string; } interface C extends A, B { propC: string; }
a) This is invalid syntax
b) This is a correct way to extend multiple interfaces
c) Only one interface can be extended
d) Interfaces A and B cannot be extended at once
Can an interface extend a class in TypeScript?
a) Yes, interfaces can extend classes in TypeScript
b) No, interfaces cannot extend classes
c) Yes, but only abstract classes
d) No, only classes can extend other classes
Which of the following is the result of extending an interface with another interface?
a) The properties of the child interface are merged with the parent
b) The child interface overrides the parent interface
c) The child interface cannot have properties
d) The parent interface loses its properties
2. Intersection and Union of Interfaces
What does the intersection of two interfaces do in TypeScript?
a) Creates a union type from the two interfaces
b) Merges properties from both interfaces
c) Creates a type where the properties of both interfaces are required
d) Removes duplicate properties between the interfaces
Which symbol is used for creating an intersection type between two interfaces in TypeScript?
a) &
b) |
c) $
d) *
What is the output type of the following code? interface A { name: string; } interface B { age: number; } type C = A & B;
a) { name: string; age: number; }
b) { name: string; }
c) { age: number; }
d) { name: string; age: string; }
What does the union of two interfaces mean in TypeScript?
a) Properties of either interface are required, but not both
b) The properties of both interfaces must exist in the object
c) It merges the properties from both interfaces
d) It creates a new interface with no properties
Which symbol is used for creating a union type between two interfaces in TypeScript?
a) &
b) |
c) $
d) ^
Which of the following creates a valid union type between two interfaces in TypeScript? interface A { name: string; } interface B { age: number; } type C = A | B;
a) This creates a type where name or age is required
b) This is invalid syntax
c) This creates a new interface with properties name and age
d) This type requires both name and age properties
3. Mapped Types and Conditional Types
What is a mapped type in TypeScript?
a) A type that iterates over an object’s properties
b) A type that defines a new structure for an object
c) A type used to define a set of possible values
d) A type used to convert a class to an interface
Which of the following creates a mapped type for all properties in the object as string type? type Stringify<T> = { [K in keyof T]: string };
a) It changes the type of all properties to string
b) It converts all properties to nullable
c) It adds a new property called K to the object
d) It removes all properties of the object
What does the keyof keyword do in TypeScript?
a) It allows you to iterate over all values of an object
b) It gives you the type of an object’s keys
c) It returns the value type of an object’s key
d) It marks an object property as optional
What is the purpose of conditional types in TypeScript?
a) To define types that change based on conditions
b) To define types for union types only
c) To create union types with specific conditions
d) To loop over an object and generate new types
Which of the following is a conditional type that checks if T is a string? type IsString<T> = T extends string ? 'Yes' : 'No';
a) It returns ‘Yes’ if T is a string, otherwise ‘No’
b) It checks if T is a string and throws an error
c) It returns ‘No’ for all types
d) It always returns ‘Yes’
How do you create a conditional type that checks if a type extends another type?
a) Using the extends keyword inside the type definition
b) Using the if keyword inside the type definition
c) Using the match keyword inside the type definition
d) Using the check keyword inside the type definition
What is the output type of the following code?typescriptCopy codetype IsNumber<T> = T extends number ? 'Yes' : 'No'; type Test = IsNumber<string>;
a) ‘Yes’
b) ‘No’
c) string
d) number
What does the mapped type { [K in keyof T]: string } do?
a) Converts all properties of T to strings
b) Makes all properties of T optional
c) Creates a new type with the same properties as T, but of type string
What is the purpose of the Readonly<T> utility type?
a) It makes all properties in T optional
b) It ensures all properties in T cannot be modified
c) It makes all properties in T mutable
d) It defines new properties in T
How does the Pick<T, K> utility type work?
a) It makes properties from T of type K
b) It picks properties from T specified in K
c) It removes properties from T that are in K
d) It picks properties from K and adds them to T
What does the Record<K, T> utility type do?
a) It creates an object with keys of type K and values of type T
b) It creates a set of keys from K
c) It ensures the keys in T are of type K
d) It creates a new interface with all properties of T
Which of the following code demonstrates the use of the Partial<T> utility type?t interface User { name: string; age: number; } const partialUser: Partial<User> = { name: "Alice" };
a) Partial<T> makes name and age optional
b) Partial<T> makes name optional
c) Partial<T> makes age optional
d) Partial<T> ensures all properties must be provided
Which utility type makes all properties in an object immutable?
a) Partial
b) Readonly
c) Pick
d) Record
Which of the following is true about the Pick<T, K> utility type?
a) It removes properties from T based on K
b) It creates a new type with only the properties of T that are in K
c) It makes the properties of T optional
d) It ensures properties of T are readonly
What will be the output of the following code using Record<K, T>? type MyRecord = Record<string, number>;
a) A record with string keys and number values
b) A record with number keys and string values
c) A string with numbers concatenated
d) An array of numbers
Which utility type would you use to ensure that a type’s properties cannot be changed after initialization?
a) Pick
b) Partial
c) Readonly
d) Record
What does the utility type Record<string, any> define?
a) An object with string keys and any type of values
b) An array of strings
c) An object where values are restricted to a certain type
d) A string with any value
Answers
QNo
Answer
1
a) extends
2
b) Yes, interfaces can extend multiple interfaces
3
a) TypeScript throws a compilation error
4
b) This is a correct way to extend multiple interfaces
5
a) Yes, interfaces can extend classes in TypeScript
6
a) The properties of the child interface are merged with the parent
7
b) Merges properties from both interfaces
8
a) &
9
a) { name: string; age: number; }
10
a) Properties of either interface are required, but not both
11
b)
12
a) This creates a type where name or age is required
13
a) A type that iterates over an object’s properties
14
a) It changes the type of all properties to string
15
b) It gives you the type of an object’s keys
16
a) To define types that change based on conditions
17
a) It returns ‘Yes’ if T is a string, otherwise ‘No’
18
a) Using the extends keyword inside the type definition
19
b) ‘No’
20
c) Creates a new type with the same properties as T, but of type string
21
a) Makes all properties in T optional
22
b) It ensures all properties in T cannot be modified
23
b) It picks properties from T specified in K
24
a) It creates an object with keys of type K and values of type T
25
a) Partial<T> makes name and age optional
26
b) Readonly
27
b) It creates a new type with only the properties of T that are in K
28
a) A record with string keys and number values
29
c) Readonly
30
a) An object with string keys and any type of values