Welcome to the fourth and final chapter of this article series. In the previous articles, we learned about utility types and saw examples with explanations. In this chapter, we will cover the last utility types and add some wrapping words.

Gili Yaniv
4 min readMar 19, 2021

InstanceType<T>

Constructs a type consisting of the instance type of a constructor function in T.

InstanceType<T>

When using InstanceType<T> we are getting the instance type of the constructor of T. It will work only with classes that have a constructor.
We can see from the example that variable x holds the properties from class C.
on the contrary, variable y hold unfamiliar values z and flag, and the compiler shows it as an error.

When should you use it?
When you want to create a type from the constructor arguments of T, Make it easier to arrange the data in a dedicated object.
More about InstanceType<T> in the official docs.

ThisParameterType<T>

Extracts the type of the this parameter for a function type, or unknown if the function type has no this parameter.

ThisParameterType<T>

I confuse that this one took me a while to fully understand. The description of it is kind of blurry and passing this between functions can always cause a mess in my mind. When using ThisParameterType<T> you are getting back the type of the this value of function T. For example in line 17 when we’re calling numberToString(123) we’re passing a number that is acceptable by toHex(). For the same reason when passing a string in line 18 we’re getting an error. Notice that toHex() specifying this type as a number. getRandom() on the other hand doesn’t do it so we can pass a string or a boolean that may cause runtime error when trying to multiply it with a random number.

When should you use it?
When you’re working with a function and you want to get the this value of it as a type.
More about ThisParameterType<T> in the official docs.

OmitThisParameter<T>

Removes the this parameter from T.

OmitThisParameter<T>

Similar to the previous utility ThisParameterType<T>, We can wrap a function with OmitThisParameter<T>, and we’ll get in return the function T type. The difference here is that now we’re omitting the this property, meaning we aren't allowed to pass one as the argument.
If we’ll take a look at the example we can see that although we aren’t passing an argument to toHex() we still get in return the value 5. That's happening because we used bind(5) and stored it in fiveToHex variable. If we’ll try to pass any argument we won’t be allowed because of the use of OmitThisParameter<T>. The expected return type is a function without arguments and with T return type.

When should you use it?
It’s a very useful utility type for cases you want to protect the this property of your function and disallow it to be overridden.
More about OmitThisParameter<T> in the official docs.

Typescript is evolving and always getting in sync with the community demands, Releasing new features and tools. Utility Types are a great way for keeping your code cleaner and consistent when working in large squads. I can tell from my experience that I used to write a class and an interface to enforce it, making me kind of a boilerplate and the requirement of keeping the class and its interface always in sync. But since I started to use the Utility Types I was always in sync with my classes and functions and my colleges always know what to expect.

I hope you find this article series useful. Please share your comments and thoughts about it and any suggestions for other topics you would like me to cover.

Thank you so much for your time and go play with the new types :)

--

--

No responses yet