Loading video...

Video Failed to Load

Go Home

Some VINTAGE TypeScript magic. Here's how to create a type-safe version of Object.assign, with an arbitrary number of objects.

38,024 views • 2 years ago •via X (Twitter)

10 Comments

Beppo's profile picture
Beppo2 years ago

Can anyone spot the bug in the code? If one argument of the merge has a partial property that is present in another obj. This type will create a wrong type and this could lead to bugs that are hard to spot. That's why most libs just intersect the objs

Matt Pocock's profile picture
Matt Pocock2 years ago

Great stuff! I suppose that filtering out partial properties during the Omit step would make sense.

eskimojo's profile picture
eskimojo2 years ago

honestly i don't find myself using Object.assign to mix different shapes, I'd rather use spread for that what i do tend to do is something like this (appears a couple times in the RTK codebase too)

Dan Gamble's profile picture
Dan Gamble2 years ago

That’s super insightful and pretty simple! Definitely need learn more about infer

CJ's profile picture
CJ2 years ago

Thank you so much for the explanation! Even the utility libs like lodash and remeda haven't solved this and are using brute force.

Bernardo Garcés's profile picture
Bernardo Garcés2 years ago

Nit, but using `object` means you can pass to your type something weird like a function `() => {}`. I'd have gone for `Record<string | number | symbol, unknown>`.

Uğur's profile picture
Uğur2 years ago

Pretty clever, love it ❤️

nitriques's profile picture
nitriques2 years ago

Very good Matt ! Prettify ftw (I will steal this)

Wollantine's profile picture
Wollantine2 years ago

Any idea if it is possible to type the merge of a generic [key: string]: string record with a value of a different type?

Fernando's profile picture
Fernando1 year ago

@HeyGenLabs translate to Spanish

Related Videos