JS Built-ins
TypeScriptintermediate

Utility Types β€” hasAllKeys<T>

πŸ“– Usage Example
type RequiredKeys<T> = {
  [K in keyof T]-?: undefined extends T[K] ? never : K
}[keyof T]
// Extracts only the required (non-optional) keys from T

Mapped types with -? remove optionality, enabling you to compute which keys are required.

Runtime Key Checking

Challenge: Implement hasAllKeys<T extends object>(obj: unknown, keys: (keyof T)[]): obj is T β€” a type guard checking that obj has all specified keys.

hasAllKeys<{a:number}>(obj, ['a'])  // β†’ true if obj.a exists
πŸ’‘ Hints (1)
  • β€’ Use the `in` operator to check key existence.