Skip to content

Instantly share code, notes, and snippets.

@msereniti
Last active February 4, 2022 09:40
Show Gist options
  • Save msereniti/44b4aaa1d95fd5327109ae2dfc16643f to your computer and use it in GitHub Desktop.
Save msereniti/44b4aaa1d95fd5327109ae2dfc16643f to your computer and use it in GitHub Desktop.
Typescript shallow merge multiple objects
type ArrShift<Arr extends any[]> = Arr extends [skip: any, ...use: infer Use]
? Use
: Arr;
type ShallowMerge<Args extends any[]> = Args extends [any, any, any, ...any] ? Args[0] & ShallowMerge<ArrShift<Args>> : Args extends [any, any] ? Args[0] & Args[1] : Args[0]
type X = ShallowMerge<[{ x: 1 }, { y: 2 }, { z: 3}, { w: 4}]>
const x = 0 as any as X
x.x
x.y
x.z
x.w
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment