You can see examples in the pictures and more demo codes in the rest of post.
![](https://kenanhancer.com/wp-content/uploads/2020/11/Utility-Types-1024x793.jpeg)
Type Inference
![](https://kenanhancer.com/wp-content/uploads/2020/06/Screenshot-2020-06-07-at-00.26.41-1024x237.png)
ConstructorParameters<T>
Syntax
![](https://kenanhancer.com/wp-content/uploads/2020/06/Screenshot-2020-06-07-at-00.27.36-1024x95.png)
Examples
![](https://kenanhancer.com/wp-content/uploads/2020/06/Screenshot-2020-06-07-at-00.27.18-1024x108.png)
InstanceType<T>
Syntax
![](https://kenanhancer.com/wp-content/uploads/2020/06/Screenshot-2020-06-07-at-00.28.18-1024x106.png)
Examples
![](https://kenanhancer.com/wp-content/uploads/2020/06/Screenshot-2020-06-07-at-00.28.46-1024x330.png)
ReturnType<T>
Syntax
![](https://kenanhancer.com/wp-content/uploads/2020/06/Screenshot-2020-06-07-at-00.29.31-1024x115.png)
Examples
![](https://kenanhancer.com/wp-content/uploads/2020/06/Screenshot-2020-06-07-at-00.29.48-1024x336.png)
Parameters<T>
Syntax
![](https://kenanhancer.com/wp-content/uploads/2020/06/Screenshot-2020-06-07-at-00.30.28-1024x109.png)
Examples
![](https://kenanhancer.com/wp-content/uploads/2020/06/Screenshot-2020-06-07-at-00.30.46-1024x257.png)
NonNullable<T>
Syntax
![](https://kenanhancer.com/wp-content/uploads/2020/06/Screenshot-2020-06-07-at-00.31.21-1024x174.png)
Examples
![](https://kenanhancer.com/wp-content/uploads/2020/06/Screenshot-2020-06-07-at-00.31.41-1024x247.png)
Extract<T, U>
Syntax
![](https://kenanhancer.com/wp-content/uploads/2020/06/Screenshot-2020-06-07-at-00.32.31-1024x207.png)
Examples
![](https://kenanhancer.com/wp-content/uploads/2020/06/Screenshot-2020-06-07-at-00.33.46-1024x231.png)
Exclude<T, U>
Syntax
![](https://kenanhancer.com/wp-content/uploads/2020/06/Screenshot-2020-06-07-at-00.34.15-1024x210.png)
Examples
![](https://kenanhancer.com/wp-content/uploads/2020/06/Screenshot-2020-06-07-at-00.34.52-1024x245.png)
Demo 1
Demo 2
As you can see in the following screenshot, hovering with mouse cursor on PersonWithOnlyString
Type Alias, VSCode shows intellisense. It means that Pick<T>
Utility Type created a new Type Alias like interface with 3 fields.
![](https://kenanhancer.com/wp-content/uploads/2020/06/Screenshot-2020-06-04-at-11.58.42-1024x206.png)
Notice that many features like Type Alias, Union Types, Literal Types, Mapped Types etc. in TypeScript are valid in compile-time not run-time. It means that let's say you created a Type Alias with three fields and write code according to this type, but if you try to remove one of the three fields in object in run-time, there will not be any exception or error in the application.
As a result, we create these features to help TypeScript compiler.
Demo 3
Notice that Pick<T>
and Partial<T>
are used together in the following demo.
Partial<T>
looks like following screenshot in the compile-time. Notice that every field in PartialPersonWithOnlyString
type can be assigned undefined.
![](https://kenanhancer.com/wp-content/uploads/2020/06/Screenshot-2020-06-06-at-23.02.43-1024x259.png)
Demo 4
Notice that Pick<T>
Utility Type is used with my custom FilterType
Type Alias. The aim is creating a new sub type from Person
interface. But, this time I didn't want to create a new sub type according to field name, new subtype is created according to field type. As you can see in the following screenshot, SubType<Person, string>
created a new Type Alias with string fields.
![](https://kenanhancer.com/wp-content/uploads/2020/06/Screenshot-2020-06-04-at-12.14.33-1024x253.png)