Open In App

How to Check Types in Typescript?

Last Updated : 07 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Checking types in TypeScript involves methods like typeof for primitive types, instanceof for class instances, and custom type guards for complex type validation. These techniques help ensure variables are correctly typed, improving code safety, and readability, and preventing runtime errors.Here are several approaches to check types in TypeScript:

1. Using the typeof operator

The typeof operator is a straightforward way to determine the data type of a value. It returns a string representing the type, such as "string," "number," or "boolean."

Example: Checking Types with typeof

In this code snippet, we demonstrate how to check the types of variables num and str using the typeof operator in TypeScript.

TypeScript
let num: number = 42;
let str: string = "Hello, TypeScript";

if (typeof num === "number") {
  console.log("num is a number");
}

if (typeof str === "string") {
  console.log("str is a string");
}

Output:

num is a number
str is a string

2. Using the instanceof Operator

The instanceof operator in TypeScript verifies if an object inherits from a specific class, allowing you to check object lineage. This is valuable for working with custom classes.

Example: Checking Instances with instanceof

In this example, we define a Person class with name and age properties. We create an instance of Person called person1 and use the instanceof operator to check if person1 is an instance of the Person class. If it is, it logs a message confirming it.

TypeScript
class Person {
  name: string;
  age: number;

  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }
}

let person1 = new Person("Alice", 30);

if (person1 instanceof Person) {
  console.log("person1 is an instance of Person");
}

Output:

person1 is an instance of Person

3. Using Type Guards

Type guards are excellent characteristics that help restrict a variable's type according to specific conditions. They improve code readability and enable complex type checks.

Example: Using Type Guards

In this example, we define a type guard function isString that checks if the provided value is a string. We then use this function to conditionally check if unknownValue is a string, and if it is, we safely utilize string methods like toUpperCase().

TypeScript
function isString(value: any): value is string {
  return typeof value === "string";
}

let unknownValue: any = "HELLO";

if (isString(unknownValue)) {
  console.log("unknownValue is a string:", unknownValue.toUpperCase());
}

Output:

unknownValue is a string: HELLO

FAQs - How to Check Types in Typescript?

What is the typeof operator used for in TypeScript?

The typeof operator is used to determine the data type of a value, returning a string such as "string," "number," or "boolean."

How does the instanceof operator work in TypeScript?

The instanceof operator checks if an object is an instance of a specific class, verifying object inheritance.

What are type guards in TypeScript?

Type guards are functions or conditions that help narrow down the type of a variable within a certain scope, enhancing type safety and code readability.

Can you use typeof to check for custom class instances?

No, typeof cannot check for custom class instances. For this, you should use the instanceof operator.

Why are type guards important in TypeScript?

Type guards are important because they provide a way to perform complex type checks and ensure that a variable conforms to a specific type within a given scope, thereby preventing runtime errors.


Next Article

Similar Reads