TypeScriptSyntax
Custom Type Guard
Code example
function isFish(p: Fish | Bird): p is Fish {
return "swim" in p;
}Writes a function that narrows a type.
In TypeScript, custom type guard: writes a function that narrows a type — function isFish(p: Fish | Bird): p is Fish { return "swim" in p; }
function isFish(p: Fish | Bird): p is Fish {
return "swim" in p;
}function isFish(p: Fish | Bird): p is Fish {
return "swim" in p;
}Writes a function that narrows a type.
Use function isFish(p: Fish | Bird): p is Fish { return "swim" in p; }.
Use this pattern: function isFish(p: Fish | Bird): p is Fish { return "swim" in p; }
Yes. Use Copy code and adapt the example to your program.
No. The language syntax is the same across operating systems.