---
title: "TypeScript Advanced: Types, Generics, Utility Types"
description: "Master TypeScript: advanced types, generics, utility types, decorators, and type-safe patterns for scalable codebases."
author: "Mohammad Abu Mattar"
canonical: https://mkabumattar.com/quizzes/post/typescript-advanced-quiz
---

# TypeScript Advanced: Types, Generics, Utility Types

Welcome to the TypeScript Advanced Quiz! Test your knowledge on advanced types, generics, utility types, and more. Each question has a hint and explanations for all options. Good luck!

## Questions

### 1. What is an interface in TypeScript?

- **A syntax to define the shape of an object, including properties and methods** ✅
  - Interface: structural typing, extendable, only at compile-time.
- A runtime construct used to instantiate objects with private internal state
  - Interfaces do not exist at runtime and cannot hold private state logic.
- A type alias that creates a unique nominal identity for primitive values
  - Interfaces define object shapes and use structural, not nominal, typing.
- A class-only decorator used to inject metadata into the constructor
  - Interfaces are type definitions, not decorators or metadata injectors.

**Hint:** Think about contracts.

### 2. What is a union type?

- **A type allowing a variable to hold values from one of several distinct types** ✅
  - Union: discriminated unions for pattern matching. Allows flexibility.
- A type that combines multiple object shapes into a single required structure
  - This describes an intersection type (&), not a union type (|).
- A specialized array type that can only hold a specific sequence of primitives
  - This describes a tuple type, not a union type.
- A dynamic type that bypasses the compiler checks to allow any value
  - The "any" type bypasses checks; union types remain strictly type-safe.

**Hint:** Think about multiple possibilities.

### 3. What is an intersection type?

- **A type that combines multiple existing types into one, requiring all properties** ✅
  - Intersection: for mixins, composition. Different from union.
- A type that allows choosing between one of two distinct object definitions
  - This describes a union type (|).
- A type guard that checks if an object belongs to a specific class instance
  - This refers to the "instanceof" operator or a custom type guard.
- A utility type used to remove specific keys from an existing interface
  - This describes the Omit utility type.

**Hint:** Think about combining types.

### 4. What is a generic in TypeScript?

- **A way to create reusable components that work with a variety of types** ✅
  - Generics: `<T>`, `<T extends Type>`, constraints. Powerful for reusability.
- A global type that automatically converts strings into numeric values
  - TypeScript does not perform automatic runtime type conversion.
- A naming convention for variables that do not have a defined data structure
  - Generics are a formal language feature for type parameterization.
- A tool for generating random data for unit tests and documentation
  - Generics are used for type-safe code logic, not random data generation.

**Hint:** Think about reusable with types.

### 5. What is a generic constraint?

- **A rule using the "extends" keyword to limit the types a generic can accept** ✅
  - Constraint: ensure type has properties. Improve type checker power.
- A runtime check that throws an error if a generic type is incorrectly cast
  - Constraints are compile-time rules; they do not exist at runtime.
- A method for preventing a generic class from being inherited by others
  - This refers to the "final" concept or private constructors.
- A syntax for forcing a generic to only accept nullable or undefined values
  - Constraints usually enforce requirements (like properties), not just nullability.

**Hint:** Think about restricting generics.

### 6. What is a conditional type?

- **A type that selects one of two types based on a ternary-like condition** ✅
  - Conditional types: T extends U ? X : Y. Powerful, complex.
- A runtime conditional statement used to validate user input types
  - Conditional types operate exclusively at the type level during compilation.
- A type alias that can only be accessed if a specific flag is set in tsconfig
  - Conditional types are part of the standard language, not config-gated.
- A function that returns a different type based on the value of its arguments
  - This refers to function overloading, which is distinct from conditional types.

**Hint:** Think about type-level if-else.

### 7. What is a mapped type?

- **A way to create a new type by iterating through the keys of an existing type** ✅
  - Mapped types: { [K in keyof T]: T[K] }. Create variants (readonly, partial).
- An array transformation that changes the data types of every stored element
  - This describes the JavaScript .map() method, not TypeScript mapped types.
- A specialized dictionary type that only accepts string-based coordinate keys
  - Mapped types are a generic transformation tool, not a specific coordinate type.
- A compilation step that maps TypeScript source files to JavaScript files
  - This describes the "Source Map" feature of the compiler.

**Hint:** Think about transforming type properties.

### 8. What is the Partial utility type?

- **A utility that constructs a type with all properties of T set to optional** ✅
  - Partial: { [K in keyof T]?: T[K] }. Useful for update objects.
- A utility that removes the first half of the properties from a given object
  - Partial refers to optionality, not a quantitative count of properties.
- A type that allows an object to only implement the methods of an interface
  - Partial applies to all properties, not just methods.
- A tool for splitting a single type definition across multiple source files
  - This refers to declaration merging or modularity.

**Hint:** Think about optional properties.

### 9. What is the Readonly utility type?

- **A utility that constructs a type with all properties of T set to readonly** ✅
  - Readonly prevents mutations by adding the readonly modifier to all keys.
- A type that prevents a variable from being reassigned to a different reference
  - This describes the "const" keyword in JavaScript.
- A compiler flag that prevents any changes to the project source code
  - Readonly is a type utility, not a global compiler configuration.
- A specialized interface used for reading data from external JSON files
  - Readonly can be used on any type, not just JSON-derived interfaces.

**Hint:** Think about immutability.

### 10. What is the Pick utility type?

- **A utility that creates a type by selecting a specific set of keys from T** ✅
  - Pick<T, K> allows you to extract specific properties to create a subset.
- A tool for randomly selecting a single property from a union type
  - Pick is a deterministic type selection tool, not a randomizer.
- A function that extracts a value from an object using a string key
  - Pick is a type utility, not a runtime function.
- A utility that removes the specified keys from an object type T
  - This describes the Omit utility type.

**Hint:** Think about selecting properties.

### 11. What is the Omit utility type?

- **A utility that creates a type by picking all properties from T and then removing K** ✅
  - Omit: remove keys. Pick<T, Exclude<keyof T, K>>.
- A tool for ignoring type errors in a specific block of TypeScript code
  - This refers to // @ts-ignore, not the Omit utility type.
- A utility that converts all mandatory properties into optional properties
  - This describes the Partial utility type.
- A syntax used to exclude a specific file from the compilation process
  - Exclusion is handled in tsconfig.json, not via the Omit type.

**Hint:** Think about excluding properties.

### 12. What is the Record utility type?

- **A utility used to construct an object type with specific keys and a specific value type** ✅
  - Record: { [P in K]: T }. Useful for lookup tables and dictionaries.
- A persistent data structure that saves type information into a database
  - Record is a compile-time type, not a data storage mechanism.
- A type that logs every change made to an object during runtime execution
  - TypeScript types cannot perform runtime logging or observation.
- A utility that converts an object into an array of its key-value pairs
  - This describes Object.entries(), not the Record utility type.

**Hint:** Think about key-value mapping.

### 13. What is the typeof operator in TypeScript?

- **An operator used in a type context to extract the type of a variable or property** ✅
  - typeof: extract types from values. Useful with const assertions.
- A runtime function that returns a string representing the primitive data type
  - This describes the JavaScript typeof operator, which behaves differently.
- A type guard that checks if a class implements a specific interface
  - This is not what the typeof operator does in a type context.
- A keyword used to define a new type based on an existing JavaScript class
  - While related, typeof extracts the type of a value, not just classes.

**Hint:** Think about extracting types.

### 14. What is the keyof operator in TypeScript?

- **An operator that produces a union of string or numeric literal keys from an object** ✅
  - keyof: property access validation. Used with generics to prevent undefined keys.
- A runtime method for retrieving an array of all keys present in an object
  - This describes Object.keys(), which returns values at runtime.
- A utility for checking if a specific key exists within a JSON configuration
  - keyof is a type-level operator for definitions, not a data validator.
- A keyword used to unlock private properties in an inherited TypeScript class
  - Privacy modifiers cannot be bypassed using the keyof operator.

**Hint:** Get property names as type?

### 15. What is the Exclude utility type?

- **A utility that removes types from a union that are assignable to a second type** ✅
  - Exclude: conditional types. Used for filtering members out of a union.
- A utility that removes all null and undefined values from a given type
  - This describes the NonNullable utility type.
- A keyword used to prevent a specific file from being imported into a module
  - Exclude is a type filter, not a module import restriction.
- A utility that deletes specific properties from an object interface
  - This describes the Omit utility type.

**Hint:** Remove types from union?

### 16. What is the Extract utility type?

- **A utility that selects members of a union that are assignable to a second type** ✅
  - Extract: conditional types. Extract<T, U> keeps matching union members.
- A tool for pulling the documentation comments out of a TypeScript file
  - This refers to documentation generators like TSDoc.
- A utility that retrieves the first element from a tuple or array type
  - Extract is for unions, not for indexing arrays.
- A method for copying the properties of one class into another class
  - This refers to mixins or inheritance, not the Extract utility.

**Hint:** Keep matching union members?

### 17. What is the NonNullable utility type?

- **A utility that constructs a type by excluding null and undefined from T** ✅
  - NonNullable: defensive programming for null-safety patterns.
- A compiler setting that throws an error if a variable is assigned a null value
  - This describes the "strictNullChecks" flag in tsconfig.json.
- A type that forces a variable to always have a default numeric value
  - NonNullable only filters types; it does not provide default values.
- A runtime check that ensures a database response is not empty
  - Types are removed during compilation and cannot check runtime responses.

**Hint:** Remove null and undefined?

### 18. What is the Parameters utility type?

- **A utility that extracts the types of arguments from a function type as a tuple** ✅
  - Parameters: reflect function signature. Useful for higher-order functions.
- A utility that generates a list of all variables declared within a function
  - TypeScript cannot extract local internal variables as a type.
- A keyword used to define the maximum number of arguments a function can take
  - TypeScript uses tuple lengths or rest parameters for this, not a utility type.
- A utility that extracts the return type of a given function definition
  - This describes the ReturnType utility type.

**Hint:** Extract function parameters?

### 19. What is the ReturnType utility type?

- **A utility that extracts the return type of a function type T** ✅
  - ReturnType: function type introspection. Essential for type-safe composition.
- A tool that automatically writes return statements for empty functions
  - TypeScript does not generate runtime code for function bodies.
- A utility that converts a synchronous function type into an asynchronous one
  - This would require wrapping the return in a Promise, not just ReturnType.
- A type that enforces a function must always return a string literal
  - ReturnType extracts whatever the function currently returns.

**Hint:** Extract function return type?

### 20. What is the InstanceType utility type?

- **A utility that extracts the instance type from a constructor function type** ✅
  - InstanceType: constructor introspection. Used in factory patterns.
- A utility that returns the name of a class as a string literal type
  - InstanceType returns the shape of the instance, not its name.
- A check that confirms if a specific object is an instance of a class
  - This describes the runtime "instanceof" operator.
- A utility that creates a new instance of a class using default parameters
  - InstanceType is a type-level operation, not a runtime instantiation tool.

**Hint:** Get instance from constructor?

### 21. What is the infer keyword in TypeScript?

- **A keyword used within a conditional type to declare a type variable to be extracted** ✅
  - infer: enables type introspection by capturing types from generic constraints.
- A compiler hint that allows TypeScript to guess the type of a variable
  - This describes "automatic type inference," which does not use the "infer" keyword.
- A syntax used to cast a value to a different type without validation
  - This describes type assertion (as Type) or the "any" type.
- A keyword that forces the compiler to ignore a specific type mismatch
  - The "infer" keyword is used for constructive type logic, not error suppression.

**Hint:** Extract types within conditionals?

### 22. What is a template literal type?

- **A type that uses template literal syntax to build new string types** ✅
  - Template literal types allow string manipulation like Capitalize and path joining.
- A JavaScript string that contains embedded expressions for runtime evaluation
  - This describes standard JavaScript template strings, not the type system feature.
- A specialized type used to define the layout of an HTML template
  - Template literal types are for string types, not for HTML DOM structures.
- A utility that converts complex objects into formatted JSON strings
  - This describes JSON.stringify(), which is a runtime function.

**Hint:** String manipulation at type level?

### 23. What is a const assertion (as const)?

- **A suffix that tells the compiler to treat a literal as its most specific type** ✅
  - as const: prevents widening (e.g., "hello" stays "hello" instead of string).
- A keyword used to declare a variable that cannot be changed at runtime
  - This describes the "const" variable declaration.
- A method for freezing a JavaScript object to prevent property additions
  - This describes Object.freeze(), which is a runtime operation.
- A tool for converting a dynamic array into a fixed-length linked list
  - Const assertions create readonly tuples, not linked list structures.

**Hint:** Freeze literal types?

### 24. What is the satisfies operator in TypeScript?

- **An operator used to validate a value matches a type while preserving its narrowest type** ✅
  - satisfies: ensures a value fits a schema without widening it to the schema type.
- A legacy operator replaced by the "as" keyword in modern versions
  - The satisfies operator is a modern addition (4.9+) and complements "as".
- A runtime check that confirms a variable satisfies a specific interface
  - Like most TypeScript features, satisfies is a compile-time only check.
- A method for ensuring that a generic type always has a default value
  - Default types are defined in the generic declaration, not via satisfies.

**Hint:** Validate type without losing inference?

### 25. What are recursive types in TypeScript?

- **Types that refer to themselves in their own definition to describe nested structures** ✅
  - Recursive types are essential for ASTs, JSON structures, and deep trees.
- Functions that call themselves until they reach a base condition
  - This describes runtime recursion, not the TypeScript type system feature.
- A type alias that causes the compiler to enter an infinite loop
  - TypeScript has safety limits to prevent infinite recursion during type checking.
- A system for automatically generating types from circular JSON data
  - Recursive types define structure; they do not generate code from data.

**Hint:** Self-referential type definitions?

### 26. What is a discriminated union?

- **A union of types that share a common literal property used for narrowing** ✅
  - Discriminated unions use a "tag" (like type or kind) for safe type guarding.
- A collection of types that are not allowed to be used in the same module
  - The term "discriminated" refers to the ability to tell them apart, not a restriction.
- An intersection of types that requires every property to be unique
  - This is not a concept in TypeScript; intersections combine, not differentiate.
- A list of deprecated types that are excluded from the final build
  - All types are removed from the final build; discrimination is a narrowing pattern.

**Hint:** Pattern matching on shared property?

### 27. What is a user-defined type guard?

- **A function that uses a type predicate to narrow a type within a conditional block** ✅
  - Type guards use the "arg is Type" syntax to inform the compiler of a check.
- A configuration setting in tsconfig that restricts the use of the "any" type
  - This describes the "noImplicitAny" flag.
- A private method used to protect a class property from external access
  - This refers to the "private" or "#" modifier.
- A runtime middleware that blocks invalid API requests before they reach code
  - Type guards are compile-time helpers that rely on runtime boolean checks.

**Hint:** Custom type narrowing function?

### 28. What is declaration merging in TypeScript?

- **The ability for the compiler to join multiple declarations with the same name** ✅
  - Declaration merging allows multiple interfaces with the same name to combine.
- A naming conflict that prevents the project from being compiled
  - While normally an error, declaration merging is a specific exception for types.
- The process of minifying multiple TypeScript files into one JavaScript file
  - This is called bundling or minification.
- A tool for combining two different versions of the TypeScript compiler
  - Declaration merging applies to code entities (interfaces, namespaces).

**Hint:** Combine multiple declarations?

### 29. What is module augmentation?

- **A way to add new type definitions to an existing external module** ✅
  - Module augmentation allows extending third-party library types (like Express).
- An automated process that increases the performance of imported modules
  - Augmentation is a type-system feature and does not affect runtime performance.
- A security feature that prevents modules from being modified at runtime
  - This describes object freezing or sealing, not module augmentation.
- A method for importing multiple modules using a single wildcard character
  - This describes "import * as name", which is standard importing.

**Hint:** Extend library types without modifying source?

### 30. What is the Awaited utility type?

- **A utility that recursively unwraps a type to retrieve the non-promise result** ✅
  - Awaited extracts the final resolved value from Promises, even if nested.
- A keyword used to pause the execution of a function until a promise resolves
  - This describes the "await" keyword, not the "Awaited" utility type.
- A type that forces a function to always return a pending Promise
  - Awaited unwraps promises; it does not wrap them.
- A utility that limits how long a compiler will wait for type resolution
  - This describes a compiler timeout, not a type-level utility.

**Hint:** Unwrap Promise types recursively?
