Today we discuss Merge
It’s quite convenient to use it in combination with ES6 spread operator for objects.
Let’s try it out 🚀
If we have the same key in both objects, we use if from the second object. Otherwise, we get it from the first object.
So let’s iterate over all keys of two objects in TypeScript and check whether we have a key in second object. If so, we extract value from the second object. Otherwise, we get value from the first object – https://tsplay.dev/Nl0BlN
Unfortunately, we have an error when we use
F[K] as TypeScript doesn’t know in advance if
K is a key of first object
F. Let’s add the condition explicitly:
This workaround works and that’s actually the solution – https://tsplay.dev/wRpM7m
Thank you for your time and have a wonderful evening 🌇typescriptmedium