Custom Predicate Functions for Type Guards
1234567891011121314151617181920212223interface 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(); }
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:
- Define the interface you want to check for;
- Write a function that accepts an
unknownor general type as its parameter; - 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; - Annotate the function's return type with
value is Typeto inform TypeScript of the type guarantee when the function returnstrue; - 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.
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Awesome!
Completion rate improved to 5.88
Custom Predicate Functions for Type Guards
Deslize para mostrar o menu
1234567891011121314151617181920212223interface 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(); }
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:
- Define the interface you want to check for;
- Write a function that accepts an
unknownor general type as its parameter; - 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; - Annotate the function's return type with
value is Typeto inform TypeScript of the type guarantee when the function returnstrue; - 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.
Obrigado pelo seu feedback!