Extract under the hood

Example of Promise.all use

Today we discuss Type Lookup

This is quite a rare case but if you need to match every element inside a union type by its field type (and you know in advance which one), thatโ€™s what you need.

Letโ€™s take a look ๐Ÿ‘€

Filterting

This solution looks like what we did in Exclude under the hood:

Exclude solution

We used Distributive Conditional Types which helped us filtering out the elements we need.

Here, we need to remove not matched (with type) elements of a union. Or in other words (if you donโ€™t like double negations as me ๐Ÿ˜…), we need to pick the union elements of a specific structure. We know in advance that the field is type. Letโ€™s try:

Solution, version 1

Also, if youโ€™re familiar with internal type Extract (itโ€™s the opposite what Exclude does), you can apply it here:

Solution, version 2

Great job ๐Ÿ”ฅ

Please, donโ€™t hesitate to go to the test cases in Playground

Have a wonderful day ๐ŸŒ‡

typescriptmedium

Let's chat and have fun ๐Ÿง‘โ€๐Ÿ’ป๐Ÿ‘ฉโ€๐Ÿ’ป

No more often than once a week I write ๐Ÿ“„ about TypeScript ๐Ÿ’ช, video playback ๐Ÿ“บ and frontend related topics. You can unsubscribe in any time โ†˜๏ธ

ยฉ 2021, Built with Gatsby