DEV Community

Cover image for Illustrated Typescript: is keyword
Behzad Ali Mohammad Zad
Behzad Ali Mohammad Zad

Posted on • Updated on

Illustrated Typescript: is keyword

This is my first article and I'm trying to visualize my articles and called it Illustrated series.

I hope the attached photos are really better than 1000 words for you.

What is is keyword in Typescript? Let's see:

Imagine, you want to publish a lib that contains some utility functions like below:

User-Defined Type Guards

We do not have any access to Date methods because the function returns a boolean and why we should access to Date methods?

Typescript performs User-Defined Type Guards and it would be better if we performed a type checked:

User-Defined Type Guards

If the isDate function returns true, TypeScript will narrow the type to Date in if block and now, we have access to Date methods.

Try it online.

Have a good day/night with Typescript.

Top comments (0)