Checking Object Type in TypeScript

When working with TypeScript, it’s often necessary to determine the type of a variable. In particular, you might need to check whether a variable is of object type. Fortunately, TypeScript provides a straightforward way to perform this type check.

The typeof Operator

In TypeScript, you can use the typeof operator to check the type of a variable. To determine if a variable is of object type, combine typeof with the object keyword.

Here’s an example:

1
2
3
4
5
6
7
const variable: any = {}; // Your variable

if (typeof variable === 'object' && variable !== null) {
  console.log('The variable is an object.');
} else {
  console.log('The variable is not an object.');
}

In this code snippet, the typeof variable === 'object' condition checks whether the type of variable is an object. Additionally, variable !== null ensures that null is not considered an object.

Specifying a Specific Object Type

If you’re looking to check for a specific object type, you can replace the any type in the variable declaration with the desired type.

Here’s an example:

1
2
3
4
5
6
7
const variable: SomeObjectType = {}; // Your variable of type SomeObjectType

if (typeof variable === 'object' && variable !== null) {
  console.log('The variable is an object of type SomeObjectType.');
} else {
  console.log('The variable is not an object of type SomeObjectType.');
}

Replace SomeObjectType with the actual type you’re expecting for your variable.

Conclusion

Checking the object type in TypeScript is essential when you want to ensure the correctness of your code. By using the typeof operator in combination with the object keyword, you can easily determine whether a variable is an object. If you’re looking for a specific object type, replace the any type with the desired type in the variable declaration.

Cheers! 🍺