Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Custom Predicate Functions for Type Guards | Type Guard Foundations
Error Handling and Type Guards in TypeScript

bookCustom Predicate Functions for Type Guards

1234567891011121314151617181920212223
interface Dog { name: string; bark(): void; } function isDog(value: unknown): value is Dog { return ( typeof value === "object" && value !== null && "name" in value && typeof (value as any).name === "string" && "bark" in value && typeof (value as any).bark === "function" ); } // Usage example: const pet: unknown = { name: "Rex", bark: () => console.log("Woof!") }; if (isDog(pet)) { // TypeScript now knows pet is a Dog pet.bark(); }
copy

When you need to check if a value matches a specific interface or type—beyond what typeof, instanceof, or Array.isArray can provide—you can write a custom predicate function as a type guard. The key to custom type guards is the value is Type return type annotation, which tells TypeScript your function will return true only if the value truly matches the given type.

To create a custom predicate function, follow these steps:

  1. Define the interface you want to check for;
  2. Write a function that accepts an unknown or general type as its parameter;
  3. Use property checks (like "prop" in value) and type checks inside the function to verify that the value matches all the requirements of the interface;
  4. Annotate the function's return type with value is Type to inform TypeScript of the type guarantee when the function returns true;
  5. Use your predicate in conditional blocks to safely narrow the type.

This approach is essential when type information is not available at runtime, or when you need to check for complex shapes, such as objects with specific methods or properties. The value is Type syntax is what enables TypeScript to treat your function as a type guard, giving you robust type safety in your code.

question mark

Which of the following scenarios requires a custom predicate function with the value is Type syntax, rather than using typeof, instanceof, Array.isArray, or the in operator alone?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 5

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 5.88

bookCustom Predicate Functions for Type Guards

Svep för att visa menyn

1234567891011121314151617181920212223
interface Dog { name: string; bark(): void; } function isDog(value: unknown): value is Dog { return ( typeof value === "object" && value !== null && "name" in value && typeof (value as any).name === "string" && "bark" in value && typeof (value as any).bark === "function" ); } // Usage example: const pet: unknown = { name: "Rex", bark: () => console.log("Woof!") }; if (isDog(pet)) { // TypeScript now knows pet is a Dog pet.bark(); }
copy

When you need to check if a value matches a specific interface or type—beyond what typeof, instanceof, or Array.isArray can provide—you can write a custom predicate function as a type guard. The key to custom type guards is the value is Type return type annotation, which tells TypeScript your function will return true only if the value truly matches the given type.

To create a custom predicate function, follow these steps:

  1. Define the interface you want to check for;
  2. Write a function that accepts an unknown or general type as its parameter;
  3. Use property checks (like "prop" in value) and type checks inside the function to verify that the value matches all the requirements of the interface;
  4. Annotate the function's return type with value is Type to inform TypeScript of the type guarantee when the function returns true;
  5. Use your predicate in conditional blocks to safely narrow the type.

This approach is essential when type information is not available at runtime, or when you need to check for complex shapes, such as objects with specific methods or properties. The value is Type syntax is what enables TypeScript to treat your function as a type guard, giving you robust type safety in your code.

question mark

Which of the following scenarios requires a custom predicate function with the value is Type syntax, rather than using typeof, instanceof, Array.isArray, or the in operator alone?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 5
some-alt