Skip to main content
InterviewHubPRO
Section Overview
Q21

Question 21 of 50

Part 2: Advanced Type System & Metaprogramming (Mid-Level)

42%
TypeScript Master (50 Qs)•Part 2: Advanced Type System & Metaprogramming (Mid-Level)

Explain Filtering Utility Types: `Pick`, `Omit`, `Exclude`, `Extract`.

mid LevelCode ExampletypescriptAdvanced Type System

In Simple Words:

  • Pick<T, K>: Constructs a type by picking specific keys K from object T.
  • Omit<T, K>: Constructs a type by omitting keys K from object T.
  • Exclude<T, U>: Excludes from union T all members assignable to U.
  • Extract<T, U>: Extracts from union T all members assignable to U.
typescript
interface User { id: string; name: string; email: string; passwordHash: string; }

type PublicProfile = Omit<User, "passwordHash">; // { id, name, email }
type Credentials = Pick<User, "email">;         // { email }

type AllTypes = string | number | boolean;
type OnlyStrings = Extract<AllTypes, string>;   // string
type NoStrings = Exclude<AllTypes, string>;     // number | boolean