Swap Keys and Literal Values, Typed

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code
  • Pro

Write invert(record): given an object whose values are strings, return a new object where each value becomes a key whose value is the old key; if two keys share a value, the later one wins. Type it so the types swap too: inverting { admin: "Administrator" } declared as const gives { Administrator: "admin" }, with the literal types kept, and a record with values that aren't strings is a type error.

Your code must pass

  • Swaps keys and values
  • A later key wins when two share a value
  • Leaves record unchanged
  • Has no type errors
  • Swaps the types of keys and values
  • Each new key gives its old key
  • Rejects values that aren't strings