Open In App

What is the use of the in Operator in TypeScript ?

Last Updated : 24 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The in operator in TypeScript is used for checking whether a specified property exists in an object. It is a type-safe way to perform property existence checks, providing developers with a mechanism to ensure that certain properties are present before accessing or manipulating them.

The primary use case for the in operator is within conditional statements or type guards where the structure of an object may vary, and you want to avoid runtime errors due to missing properties

Syntax

propertyKeyOrIndex in object

Parameters:

  • propertyKeyOrIndex: The property key or array index you want to check for in the object. It can be a string, number, or symbol.
  • object: The object in which you are checking for the existence of the specified property or index.

Return Value:

It returns true or false. If the given value is present then it returns true else it returns false.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads