JS Built-ins
TypeScriptintermediate

Advanced β€” invertObject

πŸ“– Usage Example
invertObject({ a: '1', b: '2' })   // β†’ { '1': 'a', '2': 'b' }

invertObject swaps each key with its string value, creating the inverse mapping.

Advanced: Invert Object with Uniqueness Check

Challenge: Implement invertObject<T extends Record<string, string>>(obj: T): Record<string, keyof T> throwing if duplicate values exist.

invertObject({ a: '1', b: '2' })  // β†’ { '1': 'a', '2': 'b' }
invertObject({ a: '1', b: '1' })  // throws
πŸ’‘ Hints (1)
  • β€’ Check the result object before assigning to detect duplicate values.