Designing Your Types 11 exercises

Build a Stricter Omit Type

TypeScript's Omit type allows us to construct a new type by excluding specific properties from an existing type.

Interestingly, the Omit type also lets you try to omit keys that don't exist in the type you're trying to omit from.

In this example, we're trying to omit the property b from a t

Loading exercise


00:00 In this exercise, we're going to look back at a generic type that we've used before, that's omit. And omit is kind of funny because omit actually lets you try to omit keys that don't exist in the thing that you're trying to omit from. So we have an example here where we're omitting a string and we're omitting b from it.

00:19 So we have just a string left and it would be much better, wouldn't it, if b actually was constrained to be the keys of the thing that's being passed in. So what we want to do is we want to make a strict omit here.

00:33 And this strict omit is basically just going to act like omit, except it's going to fail when you pass in something that isn't available. And it should, in all other ways, behave like omit does. So your job is to try to work out how you can constrain the parameters of t and k to properly work here.

00:53 And we can think of t as the object being passed in and k as the key that you want to omit. And I think keyof is going to be useful here, as is extends. Good luck.