Chapter 8 of TypeScript delves into advanced types such as union and intersection types, literal types, type guards, nullable types, and index signatures. These concepts help in creating flexible and type-safe code.
Multiple Choice Questions (MCQs)
Section 1: Union and Intersection Types
What is a union type in TypeScript? a) A type that combines two or more types using | b) A type that restricts values to one specific type c) A type that combines multiple values into an array d) A type that enforces an intersection of types
Which of the following is a valid union type? a) let x: number | string; b) let x: number & string; c) let x: number -> string; d) let x: string | boolean | 42;
How do intersection types differ from union types in TypeScript? a) Intersection types combine types, while union types restrict to one type b) Union types combine multiple types into one, while intersection types merge all properties c) Intersection types can be used only with objects d) Union types are for primitive types only
Which of the following is an example of an intersection type? a) let x: number | string; b) let x: string & { age: number }; c) let x: number & string; d) let x: string | boolean;
In TypeScript, which operator is used to define intersection types? a) | b) & c) => d) *
Which of the following defines a union type of string and number correctly? a) let x: string | number; b) let x: string, number; c) let x: string & number; d) let x: number -> string;
What is the result of an intersection type in TypeScript? a) A single value that can be one of several types b) A value that must fulfill all types it intersects c) An array of combined types d) A new type that combines primitive types only
How would you define a union of string, boolean, and null types? a) let x: string | boolean | null; b) let x: string & boolean & null; c) let x: string -> boolean | null; d) let x: string, boolean, null;
Section 2: Literal Types and Type Guards
What are literal types in TypeScript? a) Types that represent exact values instead of general types b) Types that can only be used with numbers c) Types that define an object’s structure d) Types that define the exact type and its possible variations
Which of the following is an example of a literal type? a) let x: 10; b) let x: string; c) let x: any; d) let x: boolean;
What does TypeScript type guarding allow you to do? a) Combine two types into one b) Narrow down a type to a specific subtype c) Define complex union types d) Allow variables to be assigned any value
Which of the following is an example of a type guard in TypeScript? a) if (x instanceof Date) b) let x: number & string; c) let x: string | number; d) function checkType(val: string | number) { return val instanceof String; }
How does a type guard help narrow down types? a) It merges the types of multiple values b) It allows you to specify additional constraints c) It ensures that variables are always nullable d) It enables conditional checks to specify the exact type
In TypeScript, which of the following can be used as a type guard? a) typeof b) instanceof c) in d) All of the above
How do you implement a custom type guard in TypeScript? a) Use the as keyword to specify types b) Create a function that returns a boolean indicating the type c) Define the guard inside the constructor function d) Use type to specify guard rules
Which of the following is the correct syntax for a literal type that allows only the string ‘true’? a) let x: 'true'; b) let x: string = 'true'; c) let x: boolean = true; d) let x: 'true' | 'false';
Section 3: Nullable Types
What does the null type represent in TypeScript? a) A variable that is not defined b) A variable with a value of null c) A variable that has no type assigned d) A variable that is undefined
How do you specify that a value can be either string or null in TypeScript? a) let x: string | null; b) let x: string, null; c) let x: string & null; d) let x: null => string;
Which of the following would allow a variable to be assigned a null value in TypeScript? a) let x: string; b) let x: string | null; c) let x: undefined; d) let x: any;
What happens if you try to assign a null to a variable that is not nullable? a) It is converted to undefined b) TypeScript throws an error c) It works without issues d) The variable becomes of type null
Which of the following is correct when allowing a variable to be nullable? a) let x: null; b) let x: null | string; c) let x: string; d) let x: string => null;
How do nullable types impact type safety in TypeScript? a) They ensure all variables are assigned types b) They allow variables to have a null value, preventing errors c) They prevent null values from being assigned d) They restrict variables to specific values only
Section 4: Index Signatures
What is an index signature in TypeScript? a) A type that defines the shape of objects with dynamic keys b) A function used to access object properties c) A method for declaring arrays d) A rule for specifying an object’s property values
How do you declare an index signature in TypeScript? a) let obj: { [key: string]: number; } b) let obj: { index: string; } c) let obj: { number[]; } d) let obj: string = {} ;
Which of the following is an example of an index signature in TypeScript? a) let obj: { [key: string]: string; }; b) let obj: { key: string; value: number; }; c) let obj: { key => string; }; d) let obj: { [key, number]: string; };
Can an index signature have a type other than string or number? a) No, only string and number are allowed b) Yes, it can be symbol c) Yes, it can be boolean d) Yes, it can be any type
How does TypeScript handle index signatures when an object has dynamic properties? a) It restricts the keys to a predefined set b) It allows any key and ensures the type matches the value type c) It automatically infers the key type d) It throws an error if a dynamic key is used
What happens if you use an index signature on an object with a fixed structure? a) It overrides the fixed structure b) The object must adhere to both the fixed structure and the index signature c) It causes a runtime error d) TypeScript automatically infers the correct structure
How do you enforce a type for all properties in an object using an index signature? a) let obj: { [key: string]: any; }; b) let obj: { [key: string]: string; }; c) let obj: { [key: number]: boolean; }; d) let obj: { [key: symbol]: number; };
Which of the following is true about the use of index signatures? a) They allow defining any property type on an object b) They restrict objects to only one type of key c) They only work with arrays d) They are limited to string keys only
Answer Key
Qno
Answer
1
a) A type that combines two or more types using `
2
a) `let x: number
3
b) Union types combine multiple types into one, while intersection types merge all properties
4
b) let x: string & { age: number };
5
b) &
6
a) `let x: string
7
b) A value that must fulfill all types it intersects
8
a) `let x: string
9
a) Types that represent exact values instead of general types
10
a) let x: 10;
11
b) Narrow down a type to a specific subtype
12
a) if (x instanceof Date)
13
b) It allows you to specify additional constraints
14
d) All of the above
15
b) Create a function that returns a boolean indicating the type
16
a) let x: 'true';
17
b) A variable with a value of null
18
a) `let x: string
19
b) `let x: string
20
b) TypeScript throws an error
21
b) `let x: null
22
b) They allow variables to have a null value, preventing errors
23
a) A type that defines the shape of objects with dynamic keys
24
a) let obj: { [key: string]: number; }
25
a) let obj: { [key: string]: string; };
26
b) Yes, it can be symbol
27
b) It allows any key and ensures the type matches the value type
28
b) The object must adhere to both the fixed structure and the index signature
29
b) let obj: { [key: string]: string; };
30
a) They allow defining any property type on an object