正在加载视频...

视频加载失败

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

38,013 次观看 • 1 年前 •via X (Twitter)

10 条评论

Beppo 的头像
Beppo1 年前

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 的头像
Matt Pocock1 年前

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

eskimojo 的头像
eskimojo1 年前

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 的头像
Dan Gamble1 年前

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

CJ 的头像
CJ1 年前

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 的头像
Bernardo Garcés1 年前

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 的头像
Uğur1 年前

Pretty clever, love it ❤️

nitriques 的头像
nitriques1 年前

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

Wollantine 的头像
Wollantine1 年前

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 的头像
Fernando1 年前

@HeyGenLabs translate to Spanish

相关视频