3 ms·
:) Fair enough: function isHuman(obj: unknown): obj is Human { return !!obj && typeof obj === 'object' && typeof (obj as any).name
by jwalton 5y ago
:) Fair enough:
function isHuman(obj: unknown): obj is Human {
return !!obj &&
typeof obj === 'object' &&
typeof (obj as any).name === 'string' &&
typeof (obj as any).age === 'number';
}
This checks the shape of the object, and returns true if it's a `Human`. This will work for array or objects or classes or anything.