List an Object's Entries as Exact Pairs

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

The problem

  • medium
  • Code
  • Pro

Object.entries types every pair as [string, any], so checking the key tells the compiler nothing about the value. Export a type Entries<T>: an array of [key, value] pairs, one pair type per key of T with that key's own value type. Then write entries(obj): return obj's own [key, value] pairs in key order, typed as Entries of its input, so that inside a loop, checking key === 'name' narrows the value to the name's type, and comparing the key with a name obj doesn't have is a type error.

Your code must pass

  • Lists own [key, value] pairs in order
  • Returns an empty list for an empty object
  • Has no type errors
  • Entries is a list of exact key and value pairs
  • Checking the key narrows the value
  • Rejects a key the object doesn't have