Utility Types in Typescript - ToolBox in TypeScript  

Written by Lucas Rodrigues

linkedin-46.png

Introduction

Typescript is well known for most developers, mainly for Javascript developers, in case you don't know what TS is, it is a javascript superset where you can define types in javascript, using typescript you can avoid silly mistakes like informing wrong types, null exceptions and easier auto complete in your IDE. 

Typescript is an open source language, maintained by Microsoft, Anders Hejlsberb is the creator (he was one of the creators for Turbo Pascal, Delphi and C#).

 

Utility Types

For most of traditional languages, you need to understand what data types are, in basic courses are required to know what is a string, a number, a decimal and a Boolean, for example.

In typescript, this is the same, and TS provides a toolbox to facilitate common type transformations called Utility Types, in this article I will describe some of them, in the reference section I will share a link of the documentation where you can read more about it.

 

Partial<T>

Let us assume that we have a interface called Todo:

code-snapshot.png

 

and looking at this type we have a title and description, both are required properties, which means, it does not accept null or undefined as a value. But for some reason, you need to implement a function that accepts todo as parameter, but the properties are not required.

code-snapshot-2.png

 look that updateTodo function, has a Partial<Todo>, now you can call your function this way:

code-snapshot-3.png

Look at the second parameter where was passed an object with the description only. By doing this way, it is not necessary to create another type and you can reuse the first one by passing a "wrapper".

Another way to implement this Partial type is to recreate Todo type like this:

code-snapshot-4.png

Required<T>

The rule is the opposite of Partial<T>, in this structure you have a Todo interface where all the properties are not required, and Required<Todo> transforms, as required, all the properties that will be necessary to be informed.

 

Readonly<T>

It makes all properties as read only, once a property was assigned (in the constructor for example) no one can mutate it, this behavior is similar to Object.freeze() in Javascript.

 

Ommit<Type,Keys>

This one is quite useful in some occasions, let's assume that we have an interface called Todo like this one:

code-snapshot-5.png

For some reason, you don't want to use isValid property, to do that, you can use Ommit<T,K> like this:

code-snapshot-6.png

the result of this utility type will generate a new type without the property that you informed to omit, something like this:

code-snapshot-7.png

Pick<Type,Keys>

Pick is another utility type, this one is opposite to Ommit<T,K>, and here you inform which property in the type you want to take as a result.

code-snapshot-8.png

Conclusion

Utility types are a good tool to have in your toolbox when you are using Typescript, it is simple to use, names are self-explanatory, avoids duplicated code, several conditionals and nullable exceptions. 

This article was based on Typescript documentation, which provides more than 20 Utility Types.

 

References:

Guest User