Software Development

Why TypeScript Will Never Add Strict Omit

The Omit type from TypeScript is counter-intuitive. Let me explain why.

Take this Example type with properties x, y:

In OmitExample, I construct a new Example type without the x, and the result is as you would expect.

In OmitExample1, I remove the xyz property. But hang on, this doesn't exist on Example, so shouldn't I get a type error?

Well no you shouldn't, and that is because Omit is loose by default rather than strict.

Now before I dive into why this is true, let's first figure out how you can make Omit strict.

How to make Omit strict

Strict Omit is not built into Typescript, so we need to create a custom type StrictOmit to handle this:

This type is almost identical to Omit but ensures the second argument - the key we want to omit - is present in the first argument.

If we apply it to the example above, we now get a type error as expected!

Why Omit is loose by default

The main reason is pretty simple.

It's easy to construct a strict Omit very quickly as we've seen above.

Additionally, loose Omit actually has some valid use cases, one of which is property spreading.

Property spreading allows you to overwrite properties of one type with another.

Here's how I would construct the Spread type to make this work:

  • We take two arguments, the former is the type to be overwritten and the latter contains the properties to overwrite from the first type.
  • We intersect the second type with the first type excluding properties from the second type that exists in the first type.

Pretty cool right? Here is an example that overwrites the y property in an object from a literal type 2 to string:

In other words, the final type will look like this:

Conclusion

So this is the reason why Typescript will never include strict omit as a native type.

I hope you found this explanation useful, and I hope the implementation of StrictOmit will come in handy for your Typescript projects.


If you enjoyed this article, please make sure to Subscribe, Clap, Comment and Connect with me today! 🌐