Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Using typeof and Array.isArray | Type Guard Foundations
Error Handling and Type Guards in TypeScript

bookUsing typeof and Array.isArray

123456789101112
function printLengthOrDouble(value: string | number) { if (typeof value === "string") { // value is narrowed to string console.log("String length:", value.length); } else if (typeof value === "number") { // value is narrowed to number console.log("Double the number:", value * 2); } } printLengthOrDouble("hello"); printLengthOrDouble(10);
copy

The typeof operator is a built-in JavaScript and TypeScript mechanism for checking the type of a value at runtime. It is especially useful for distinguishing between primitive types such as string, number, boolean, and undefined. When you use typeof in a conditional, TypeScript can automatically narrow the type within each branch. However, typeof has limitations: it does not distinguish between different object types (for example, arrays vs. plain objects) and will return "object" for both. It also cannot differentiate between null and other objects, since typeof null is "object". Use typeof for primitive type checks, but not for arrays or custom objects.

12345678910111213141516
function describeInput(input: unknown) { if (Array.isArray(input)) { // input is narrowed to any[] console.log("Input is an array with length", input.length); } else if (typeof input === "object" && input !== null && "name" in input) { // input is narrowed to an object with a 'name' property // TypeScript knows 'name' exists, but not its type console.log("Object with name:", (input as { name: unknown }).name); } else { console.log("Input is neither an array nor an object with a name property."); } } describeInput([1, 2, 3]); describeInput({ name: "Alice" }); describeInput(42);
copy

Array.isArray is a built-in function that checks if a value is an array. When used in a conditional, it acts as a type guard, allowing TypeScript to treat the value as an array within that block. This is the recommended way to distinguish arrays from other objects, since typeof returns "object" for arrays as well. The in operator checks if a property exists in an object, which is useful for narrowing types when dealing with objects that may or may not have certain properties. However, neither Array.isArray nor the in operator provides information about the specific types of the array elements or the property types, so further checks may be needed for full type safety.

question mark

Which built-in type guard should you use to distinguish between a string and a number, and which should you use to determine if a value is an array?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 5.88

bookUsing typeof and Array.isArray

Swipe to show menu

123456789101112
function printLengthOrDouble(value: string | number) { if (typeof value === "string") { // value is narrowed to string console.log("String length:", value.length); } else if (typeof value === "number") { // value is narrowed to number console.log("Double the number:", value * 2); } } printLengthOrDouble("hello"); printLengthOrDouble(10);
copy

The typeof operator is a built-in JavaScript and TypeScript mechanism for checking the type of a value at runtime. It is especially useful for distinguishing between primitive types such as string, number, boolean, and undefined. When you use typeof in a conditional, TypeScript can automatically narrow the type within each branch. However, typeof has limitations: it does not distinguish between different object types (for example, arrays vs. plain objects) and will return "object" for both. It also cannot differentiate between null and other objects, since typeof null is "object". Use typeof for primitive type checks, but not for arrays or custom objects.

12345678910111213141516
function describeInput(input: unknown) { if (Array.isArray(input)) { // input is narrowed to any[] console.log("Input is an array with length", input.length); } else if (typeof input === "object" && input !== null && "name" in input) { // input is narrowed to an object with a 'name' property // TypeScript knows 'name' exists, but not its type console.log("Object with name:", (input as { name: unknown }).name); } else { console.log("Input is neither an array nor an object with a name property."); } } describeInput([1, 2, 3]); describeInput({ name: "Alice" }); describeInput(42);
copy

Array.isArray is a built-in function that checks if a value is an array. When used in a conditional, it acts as a type guard, allowing TypeScript to treat the value as an array within that block. This is the recommended way to distinguish arrays from other objects, since typeof returns "object" for arrays as well. The in operator checks if a property exists in an object, which is useful for narrowing types when dealing with objects that may or may not have certain properties. However, neither Array.isArray nor the in operator provides information about the specific types of the array elements or the property types, so further checks may be needed for full type safety.

question mark

Which built-in type guard should you use to distinguish between a string and a number, and which should you use to determine if a value is an array?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 2
some-alt