MCQs on Advanced Type Manipulations | TypeScript

Advanced type manipulations in TypeScript provide developers with powerful tools for creating flexible, scalable, and reusable types. This set of 30 MCQs explores key remapping, template literal types, recursive types, and custom utility types.


Advanced Type Manipulations in TypeScript – MCQs

1. Key Remapping in Mapped Types

  1. What is key remapping in mapped types in TypeScript?
    • a) Changing the type of a key in an object
    • b) Modifying the key names of an object
    • c) Changing the order of keys in an object
    • d) Renaming the properties of an interface
  2. Which symbol is used for key remapping in mapped types?
    • a) &
    • b) =>
    • c) in
    • d) @
  3. How can you change the key names of an object using mapped types?
    • a) By using the keyof operator
    • b) By using the as keyword in a mapped type
    • c) By using the extends keyword in an interface
    • d) By directly modifying the object’s keys
  4. What will this code do in TypeScript?
    type Remapped<T> = { [K in keyof T as `new_${string & K}`]: T[K] };
    • a) It adds a prefix new_ to all keys of T
    • b) It renames all properties of T to new_
    • c) It removes all properties of T
    • d) It adds a new property called new_ to T
  5. Which of the following is an example of remapping keys using a mapped type?
    type RenameKeys<T> = { [K in keyof T as `prefix_${string & K}`]: T[K] };
    • a) It renames each property key by adding a prefix
    • b) It replaces all the values of T with a string
    • c) It changes the types of the keys to string
    • d) It removes properties from the object
  6. Which type will this code produce?
    type Prefix<T> = { [K in keyof T as `prefix_${K}`]: T[K] };
    • a) It prefixes each property name with prefix_
    • b) It prefixes each property value with prefix_
    • c) It replaces each key with prefix_
    • d) It throws an error because of incorrect syntax

2. Template Literal Types

  1. What are template literal types used for in TypeScript?
    • a) Defining a string with variable interpolation
    • b) Creating type aliases for classes
    • c) Defining dynamic object keys
    • d) Creating union types with string literals
  2. Which of the following is a valid template literal type in TypeScript?
    • a) type T = 'Hello ' | 'World'
    • b) type T = 'prefix_' + string
    • c) type T = string | number
    • d) type T = 'prefix_' | 'suffix_'
  3. How do you define a template literal type that combines a string prefix and a value of type number?
    • a) type T = 'prefix_' | number
    • b) type T = 'prefix_' + number
    • c) type T = prefix_${number}“
    • d) type T = 'prefix_' & number
  4. What does the following template literal type represent in TypeScript?
    type Fruit = `apple_${string}`;
    • a) Any string that starts with apple_
    • b) A string literal 'apple_'
    • c) A string that exactly equals 'apple_'
    • d) A union of string literals
  5. Which of the following is a valid template literal type expression?
    type Animal = `dog_${string}`; type Plant = `plant_${string}`;
    • a) type T = ${Animal} & ${Plant};
    • b) type T = ${string} & ${Animal};
    • c) type T = dog_plant_;
    • d) type T = ${Animal} | ${Plant};
  6. How do template literal types help in dynamic string construction?
    • a) They combine different types into a fixed string literal type
    • b) They concatenate variables in a type-safe manner
    • c) They allow combining multiple types into an object
    • d) They restrict strings to predefined literals only

3. Recursive Types

  1. What is a recursive type in TypeScript?
    • a) A type that refers to itself
    • b) A type that can only be used in functions
    • c) A type that is dynamically created
    • d) A type that represents arrays
  2. How would you define a recursive type to represent a nested object structure?
    • a) type Nested = { value: string, children: Nested[] };
    • b) type Nested = { value: string, children: string[] };
    • c) type Nested = { children: Nested };
    • d) type Nested = { value: string, children: Nested };
  3. Which of the following is a recursive type used for representing a directory structure in TypeScript?
    type Directory = { name: string; subdirectories: Directory[]; };
    • a) This defines a structure with a nested list of directories
    • b) This type can only be used for file names
    • c) This type is invalid because of infinite recursion
    • d) This type is only used for a single-level directory
  4. What is the primary use case for recursive types in TypeScript?
    • a) To represent hierarchical or nested data structures
    • b) To repeat a value multiple times in a string
    • c) To avoid using classes for object creation
    • d) To map types based on conditions
  5. Can recursive types lead to infinite loops in TypeScript?
    • a) Yes, if not carefully designed
    • b) No, TypeScript handles recursion automatically
    • c) Yes, but only if the type is undefined
    • d) No, recursion is always allowed in TypeScript
  6. How do you prevent infinite recursion in a recursive type definition?
    • a) By adding a base case to the recursion
    • b) By using an interface instead of a type
    • c) By marking the recursive type as readonly
    • d) By using any as a type

4. Custom Utility Types

  1. What is the purpose of custom utility types in TypeScript?
    • a) To create reusable, flexible types for common patterns
    • b) To define types for native objects only
    • c) To handle JavaScript functions more efficiently
    • d) To limit the types to a set of predefined values
  2. Which of the following is an example of a custom utility type?
    type Without<T, U> = T extends U ? never : T;
    • a) It filters out properties of type U from T
    • b) It adds all properties of U to T
    • c) It defines a new type without any properties
    • d) It changes the types of T to U
  3. How do custom utility types enhance TypeScript’s type system?
    • a) By providing more control over how types are transformed or filtered
    • b) By limiting the use of interfaces in TypeScript
    • c) By allowing only specific values in a type
    • d) By creating types for dynamic functions only
  4. What is the purpose of keyof in custom utility types?
    • a) To get the union of all keys in a type
    • b) To extract the value types of a key in an object
    • c) To map types to keys in an object
    • d) To create new keys in an object
  5. How do you define a custom utility type that makes all properties of an object optional?
    • a) type Optional<T> = { [K in keyof T]?: T[K] };
    • b) type Optional<T> = { [K in keyof T]!: T[K] };
    • c) type Optional<T> = { [K in keyof T]?: string };
    • d) type Optional<T> = { T: any };
  6. Which custom utility type would you use to create a type with only selected properties of another type?
    • a) Pick
    • b) Record
    • c) Partial
    • d) Exclude
  7. How does the Exclude<T, U> utility type work in TypeScript?
    • a) It excludes the properties of T that are in U
    • b) It includes all properties from U into T
    • c) It makes T and U union types
    • d) It merges T and U types into one
  8. What is the role of ReturnType<T> in TypeScript?
    • a) It extracts the return type of a function T
    • b) It creates a return type for a function
    • c) It modifies the return type of a function
    • d) It creates a type for function arguments
  9. Which utility type makes all properties in an object readonly in TypeScript?
    • a) Readonly<T>
    • b) Partial<T>
    • c) Pick<T, K>
    • d) Record<K, T>
  10. How would you define a type that only contains the keys of T with the values of type string?
    • a) type StringValues<T> = { [K in keyof T]: string };
    • b) type StringValues<T> = { [K in keyof T]: number };
    • c) type StringValues<T> = { [K in keyof T]: T[K] };
    • d) type StringValues<T> = { string: T };
  11. Which of the following is a correct way to create a custom utility type for optional properties?
    • a) type Optional<T> = { [K in keyof T]?: T[K] }
    • b) type Optional<T> = { [K in keyof T]: T[K] }
    • c) type Optional<T> = { [K in keyof T]: string }
    • d) type Optional<T> = { T: any }
  12. Which utility type would you use to convert all properties of a type to undefined?
    • a) Partial
    • b) Pick
    • c) Record
    • d) Nullable

Answers

QNoAnswer
1b) Modifying the key names of an object
2c) in
3b) By using the as keyword in a mapped type
4a) It adds a prefix new_ to all keys of T
5a) It renames each property key by adding a prefix
6a) It prefixes each property name with prefix_
7a) Defining a string with variable interpolation
8b) type T = 'prefix_' + string
9c) type T = prefix_${number}“
10a) Any string that starts with apple_
11a) type T = ${Animal} & ${Plant};
12b) They concatenate variables in a type-safe manner
13a) A type that refers to itself
14a) type Nested = { value: string, children: Nested[] };
15a) This defines a structure with a nested list of directories
16a) To represent hierarchical or nested data structures
17a) Yes, if not carefully designed
18a) By adding a base case to the recursion
19a) To create reusable, flexible types for common patterns
20a) It filters out properties of type U from T
21a) By providing more control over how types are transformed or filtered
22a) To get the union of all keys in a type
23a) type Optional<T> = { [K in keyof T]?: T[K] };
24a) Pick
25a) It excludes the properties of T that are in U
26a) It extracts the return type of a function T
27a) Readonly<T>
28a) type StringValues<T> = { [K in keyof T]: string };
29a) type Optional<T> = { [K in keyof T]?: T[K] }
30a) Partial

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top