You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Generic type-safe implementation for Object.assign
It seems that it can be possible to make a type-safe `Object.assign`-like functions with TypeScript, without having to make lots of overrides.
Here's an example on the playground:
typescriptlang.org/play/index.html#src=function%20assign%3CSource%20extends%20%7B%7D%2C%20Result%20extends%20Source%3E(%0D%0A%20%20%20%20target%3A%20Source%2C%0D%0A%20%20%20%20...sources%3A%20Source%5B%5D%0D%0A)%3A%20Pick%3CResult%2C%20keyof%20Source%3E%20%7B%0D%0A%20%20%20%20return%20Object.assign(target%2C%20...sources)%3B%0D%0A%7D%0D%0A%0D%0Alet%20x%20%3D%0D%0A%20%20%20%20assign(%0D%0A%20%20%20%20%20%20%20%20%7B%20hello%3A%20%22world%22%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%20something%3A%20%22is%20good%22%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%20%22yes%22%3A%20true%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%20yes%3A%20%22hello%22%20%7D%0D%0A%20%20%20%20)%3B%0D%0A%0D%0Ax.hello%3B%0D%0Ax.world%3B%20%2F%2F%20error!%0D%0Ax.yes%3B
You can see that the compiler says `x.hello` is fine, and complains about `x.world`
0 commit comments