Merge Two Objects, Later Types Win
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Spreading { ...a, ...b } is typed as A & B, so a key in both with different types becomes never. Export a type Merge<A, B> with every key of A and B, where a key in B takes B's type and every other key keeps A's. Then write merge(a, b): return a new object with a's properties overridden by b's, leaving both unchanged, typed as Merge of the two, so merging { id: 1 } with { id: 'u_1' } gives an id that's a string, not a number.
Your code must pass
- Later values win
- Keeps keys from both
- Returns a new object, leaving both unchanged
- Has no type errors
- B's types replace A's
- merge returns the merged type