Type Transformations Workshop 0 exercises
Problem

Conditionally Extract Properties from Object

We have an object that contains several keys that contain id:


interface Example {
name: string
age: number
id: string
organisationId: string
groupId: string
}
type OnlyIdKeys<T> = unknown

Challenge

Your challenge is to write the OnlyIdKeys type helper so that it conta

Loading exercise

Transcript

0:00 In this exercise, we have an example object here with a bunch of keys in. Several of them have keys which contain IDs. We want to create a new object out of that using this type helper OnlyIdKeys to extract only the keys that contain an ID.

0:19 We've got to do some funky steps here. We have to basically take the object. We need to think about remapping over the object and possibly using new keys from it, possibly using some syntax that we've seen before. You may notice that you might need a conditional type in here somewhere too.

0:41 There are probably several solutions to this, but I've picked one that shows an interesting use case for key remapping, especially using never. Those are your clues. Good luck.