Using typeof and Array.isArray
123456789101112function 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);
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.
12345678910111213141516function 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);
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.
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Awesome!
Completion rate improved to 5.88
Using typeof and Array.isArray
Glissez pour afficher le menu
123456789101112function 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);
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.
12345678910111213141516function 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);
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.
Merci pour vos commentaires !