Video wird geladen...

Video konnte nicht geladen werden

Zur Startseite

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

38,013 Aufrufe • vor 1 Jahr •via X (Twitter)

10 Kommentare

Profilbild von Beppo
Beppovor 1 Jahr

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

Profilbild von Matt Pocock
Matt Pocockvor 1 Jahr

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

Profilbild von eskimojo
eskimojovor 1 Jahr

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)

Profilbild von Dan Gamble
Dan Gamblevor 1 Jahr

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

Profilbild von CJ
CJvor 1 Jahr

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

Profilbild von Bernardo Garcés
Bernardo Garcésvor 1 Jahr

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>`.

Profilbild von Uğur
Uğurvor 1 Jahr

Pretty clever, love it ❤️

Profilbild von nitriques
nitriquesvor 1 Jahr

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

Profilbild von Wollantine
Wollantinevor 1 Jahr

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

Profilbild von Fernando
Fernandovor 1 Jahr

@HeyGenLabs translate to Spanish

Ähnliche Videos