Build an Exact Object from Entries
Write the code, see it run, pass the checks.
The problem
- hard
- Code
Write fromEntries(entries), a typed Object.fromEntries for a list of readonly [key, value] tuples declared as const: return an object with each key set to its value, a later entry replacing an earlier one with the same key. Type it so the result is the exact object, each key with its own literal value type (entries [["id", 7], ["name", "Ada"]] give { id: 7; name: "Ada" }, not Record<string, unknown>), and so entries that aren't key-value pairs are a type error.
Your code must pass
- Builds an object from the entries
- A later entry replaces an earlier one with the same key
- Has no type errors
- Gives the exact object type
- Each key keeps its own value type
- Rejects entries that aren't key-value pairs