Custom Type Guards
A function returning x is T tells the compiler a check narrows the type.
Example
interface Cat { meow(): void }
function isCat(x: any): x is Cat { return typeof x?.meow === "function"; }
const pet: unknown = { meow() {} };
console.log(isCat(pet));Result
true
Exercise
Guard return.
function f(x: any): x string
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.