Getting friendly with TypeScript (part 4): Utility types

Sammy-Jo Wymer
REWRITE TECH by diconium
6 min readNov 15, 2022

--

This article is intended for anyone who is trying to get to grips with the world of TypeScript.

It is recommended to read part 1, part 2 and part 3 before reading this article.

This article will cover:

  • Utility Types 🥝
  • Lots of simple code examples 🪄
Photo by Any Lane: https://www.pexels.com/photo/slices-of-kiwi-on-pink-background-5946061/

In TypeScript, there are multiple built-in default types that we can use.

These are basically types that TypeScript has already created and so we don’t need to worry about creating them ourselves.

As always, examples are our friend because the more I write, the more likely I am to start talking about Jason Bateman or Jason Sudeikis. Oh, there we go.

Utility type one: Partial<T>

Remember the optional property we learned in part 2? Well, this is basically what Partial<T> does — it is the same as adding ?to all the properties.

In a nutshell: Partial<T> makes all properties optional.

simple interface example
simple interface example

The examples above show us a simple interface example. The example below shows us how Partial<T> works.

Because Partial<T> makes all properties optional, we are free to choose which of the properties we use for different variables and we don’t get an error if we don’t use them all.

Partial<T> example

Utility type two: Required<T>

This is the reverse of Partial<T>

In a nutshell: it makes all ‘optional’ properties of an interface ‘required’.

simple interface example with optional properties
simple interface example with optional properties

The examples above show a simple interface with all properties as optional. The example below shows how Required<T> works. It makes all of these properties required and will show an error if any of these properties are missing.

Required<T> example

Utility type three: Readonly<T>

In a nutshell: this is the same as Required<T> but it makes all the properties read only.

simple interface example

Above, we have another simple interface example. Below we can see how Readyonly<T> works.

Because we have made readonlyExample a Readonly type, we are not able to modify these variables as you can see in the example below.

Readonly type example

Utility type four: Pick<T, K>

With Pick<T,K>, we can pick the properties that we want to use from another type/interface.

In a nutshell: we can create a type based on another type/interface.

simple interface example

In the example above, we have another simple interface. Below, we can see the syntax forPick<T,K>:

Pick<T, K> example

Essentially, we are creating a new type E which only has the x and z properties from interface D

If you hover over type E you can see the shape it has:

helpful message from TypeScript

Utility type five: Omit<T, K>

This is the exact opposite ofPick<T, K>

In a nutshell: you choose the properties you don’t want to include in your new type/interface.

Omit<T, K> example

Again, in the above example, we are using interface D and instead of picking the variables we want to use for type F , we are selecting the variables we would like to omit.

This means that type F would have the exact same shape as type E as you can see in the example below:

another helpful message from TypeScript

Utility type six: Exclude <T, U>

This is similar to Omit<T, K> but is used for union types and not properties within types.

In a nutshell: it lets us exclude certain members from an already defined union type.

union types and Omit<T, K> example

Hopefully, you can recall from part 1 that Unions can be one of several types.

The example above is an extension of one we looked at previously. Shows is a Union type and can be one of 3 values.

JasonBatemanShows is a type created from the Shows type, but it omits the show Ted Lasso because (hopefully it’s common knowledge) he does not appear in this show. This would be Jason Sudeikis. Oh, great I’m doing it again.

Now, when we hover over JasonBatemanShows you should know what to expect:

yet another helpful TypeScript message

Utility type seven: NonNullable<T>

The name pretty much explains this one.

In a nutshell: it removes all the null or undefined elements of a type.

NonNullable type example

Above, you see that we have a Union type G which can be of type string, string[] ,undefined or null

We can remove the undefined and null types with NonNullable

NonNullable type example

Utility type eight: Record <K, T>

In a nutshell: this is a shortcut for defining an object type with a specific key and value type.

This means you can narrow down your records by only accepting specific keys or types of keys.

Record<K, T> example

In the above example, we have created a simple Record type called aRecord. This record takes string as the key and number as the value.

This type is good to use when you are mapping and only want to allow certain keys or values.

Let’s see one more example, to help it stick:

union type example

Above, we have extended our Shows Union type.

Below, we have created a new Record type which takes the Shows type as it’s key and boolean as it’s value.

Record<T, K> example

This means that the key has to be either: Ozark, Arrested Development, Ted Lasso or Stranger Things, and the value can only be true or false.

Record<T, K> example

This protects us from making any typos or adding incorrect values, which could be a pain to debug later:

typo example

TypeScript also helps us out and offers us only the valid values we can use for this property:

TypeScript being helpful YET again

We all love IntelliSense 🤤

Photo by Trang Doan: https://www.pexels.com/photo/sliced-fruits-on-tray-1132047/

So, that’s it for our beginner’s guide to TypeScript! There may be more to come in this mini-series, so please leave a comment with any TypeScript topics that you would like to be covered.

🥝🍓🥝🍓🥝🍓🥝🍓🥝🍓🥝🍓🥝🍓🥝🍓🥝🍓🥝🍓🥝🍓🥝🍓🥝🍓🥝🍓🥝🍓🥝🍓🥝

If you enjoyed this article, you might enjoy other articles from the ‘Getting friendly with…’ series:

Getting friendly with the terminal: a super-friendly beginner’s guide

Getting friendly with git: a super-friendly beginner’s guide

Getting friendly with TypeScript (part 1): a super-friendly beginner’s guide

Getting friendly with TypeScript (part 2): a super-friendly beginner’s guide

Getting friendly with TypeScript (part 3): a super-friendly beginner’s guide

Keep challenging yourself and remember, being uncomfortable means you are growing.

--

--