- Preparing search index...
- The search index is not available
Documentation
- isInstanceOf<T>(arg: unknown, typeGuardMap: { [ index in string | number | symbol]: ((arg: unknown) => arg is T[index]) }): arg is T
-
Parameters
-
arg: unknown
-
typeGuardMap: { [ index in string | number | symbol]: ((arg: unknown) => arg is T[index]) }
Returns arg is T
Convenience function to implement typeguard for an interface T
The consistency of the implementation is enforced by the compiler.
Example:
interface I { foo: number; bar: string; }
// Ok: function isI(arg: unknown): arg is I { return isInstanceOf(arg, { foo: isString, bar: isNumber, }); }
// Error (typeguard for bar is missing): function isI(arg: unknown): arg is I { return isInstanceOf(arg, { foo: isString, }); }
// Error (typeguard for bar has wrong type): function isI(arg: unknown): arg is I { return isInstanceOf(arg, { foo: isString, bar: isString, }); }
// Error (baz is not part of I): function isI(arg: unknown): arg is I { return isInstanceOf(arg, { foo: isString, bar: isNumber, baz: isBoolean, }); }