In this article, we are going to learn about the type predicates in Typescript. TypeScript is a statically typed programming language that provides many features to make your code more efficient and robust. Type predicates in TypeScript are functions that return a boolean value and are used to narrow down the type of a variable. They are primarily used in conditional blocks to check whether a variable is of a particular type and then perform specific operations accordingly. Type predicates can be defined using the “is” keyword in TypeScript.
Syntax:
function is<TypeName>(variable: any): variable is TypeName { // Return boolean value }
In the above syntax, the “TypeName” is the name of the type you want to check, and the “variable” is the variable whose type you want to narrow down. The “is” keyword specifies that the function returns a boolean value, and the “variable is TypeName” tells TypeScript that the variable is of the specified type if the function returns true.
Example 1: Type Predicate with Primitive Types: In this example, we will understand how type predicates work with primitive types. Suppose we have a function that accepts a variable of type “any” and we want to check whether it is a string or not. We can define a type predicate function for this as follows. we are checking whether the “variable” is of type “string” by using the “typeof” operator in JavaScript. If the “variable” is a string, the function will return true; otherwise, it will return false. Now, let’s use this type predicate function in a conditional block, we will check whether the “variable” is a string or not using the “isString” type predicate function and print the result of our choice.
Javascript
let variable: any = 'hello' ; if (isString(variable)) { console.log( "The given variable is type of String" ); } function isString(variable: any): variable is string { return typeof variable === 'string' ; } |
Output:
Example 2: Type Predicate with Custom Types: Let’s take another example to understand how type predicates work with custom types. Suppose we have an interface for a “Person” object. Now, let’s define a type predicate function that checks whether a variable is of type “Person”, then we will check whether the “variable” is an object that has the properties “name”, “age”, and “address” by using the “in” operator. If the “variable” is of type “Person”, the function will return true; otherwise, it will return false, and now we can use this type predicate function in a conditional block, If the function returns true, we can safely access the “city” property of the “address” object because we know that the “variable” is of type “Person”.
Javascript
interface Person { name: string; age: number; address: { street: string; city: string; zip: number; }; } function isPerson(variable: any): variable is Person { return ( typeof variable === "object" && variable !== null && "name" in variable && "age" in variable && "address" in variable ); } let variable: any = { name: "John" , age: 30, address: { street: "123 Main St" , city: "New York" , zip: 10001 }, }; if (isPerson(variable)) { console.log(variable.address.city); } |
Output:
In conclusion, type predicates in TypeScript allow you to narrow down the type of a variable within a conditional block by specifying a custom type guard function. This feature can make your code more robust and efficient by preventing runtime errors and improving the readability of your code. In this article, we discussed what type predicates are, and their syntax, and provided two working examples of how to use them with primitive types and custom types.