Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære 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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 5

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you explain how the `value is Type` syntax works in more detail?

What are some common use cases for custom type guards in TypeScript?

Can you show how to write a custom type guard for a more complex interface?

Awesome!

Completion rate improved to 5.88

bookCustom Predicate Functions for Type Guards

Sveip for å vise menyen

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 5
some-alt