Advanced Typescript. Type Guards and Differentiating Types

Artem Diashkin
Aug 11, 2020 · 3 min read

This article will demonstrate how you can check your types and instances using user-defined Type Guards and reserved keyword in Typescript in examples

Prerequisites

package.json:

As you can see on a screenshot I’ve created an src/index.ts file with a simple class.

I’m running *.ts files using npx ts-node <file-path> command.

And that’s it, that’s all you need. ✔️

Type Guards and Differentiating Types

  • typeof keyword;
  • instanceof keyword;
  • user-defined Type Guards

Let’s check how they work one-by-one.

1.1 Using “typeof” keyword

typeof keyword is commonly used for checking value types like string, number, bigint, boolean, function, object, symbol and undefined.

For describing how typeof keyword works I’ve created a new file src/differentiation/typeof.ts :

Let’s run this file and check that Typescript is ok with it:

1.2 Using "instanceof" keyword

instanceof keyword is commonly used for differentiating Classes.

For describing how instanceof keyword works I’ve created a new file src/differentiation/instanceof.ts :

Let’s run this file and check that Typescript is ok with it:

1.3 Using user-defined Type Guards

You can define your custom Type Guard by using this code snippet:

function isSomeType(var): var is SomeType {...}

What does it mean? It’s basically means that isSomeType function returns true or false, but… if it will return true , than Typescript will be sure that var is SomeType type.

Let’s look at how it works. For this example, I’ve created a new file src/differentiation/guards.ts:

Run this file and check that Typescript is ok with it:

As you can see at line 47 we are setting valueClass.foo (that could be either string or a number) to a number type variable someNumber . And Typescript is ok with that because it knows that if isNumberClass() function will return true — that means that valueClass is a NumberClass instance and foo is definitely a number.

!! Be aware of this kind of situation:

I have intentionally made a mistake in our example and now isNumberClass function returns true for a StringClass NOT a NumberClass

Typescript is NOT AWARE of your type-checking MISTAKES. It knows that isNumberClass function should return true if instance is NumberClass. But is it in reality Typescript doesn’t know.

LITSLINK

LITSLINK’s team is sharing their experience

Sign up for LITSLINK Newsletter

By LITSLINK

It's a test newsletter! Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Artem Diashkin

Written by

Java, Spring, Node.js, AdonisJs, React.js and Flutter developer

LITSLINK

LITSLINK

Hands-on up to date experience sharing of the most skilled & talented software developers right here, right now. Subscribe to learn & understand more about the Software World.

Artem Diashkin

Written by

Java, Spring, Node.js, AdonisJs, React.js and Flutter developer

LITSLINK

LITSLINK

Hands-on up to date experience sharing of the most skilled & talented software developers right here, right now. Subscribe to learn & understand more about the Software World.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app