// doesn't throw, value is typed as any[]assertIsArray(['xyz']);// doesn't throw, value is typed as string[]assertIsArray<string>(['xyz'], { valueValidator:isString });// throwsassertIsArray<string>(['xyz', 1], { valueValidator:isString });
assertIsError<T>(input: unknown, options?: ErrorMessage): asserts input is T
example
// does not throw, value is typed as ErrorassertIsError(newError());// does not throw, value is typed as ErrorassertIsError<TypeError>(newTypeError());// does not throw, value is typed as TypeErrorassertIsError<TypeError>(newTypeError());// does not throw, value is typed as MyError - if MyError inherits from Error or its sub-classesassertIsError<MyError>(newMyError());
assertIsFunction<T>(input: unknown, options?: ErrorMessage): asserts input is T
remarks
This guard works only in ES2018 and above
example
// does not throw, value is typed as FunctionassertIsFunction(() =>true);// does not throw, value is typed as () => booleanassertIsFunction<() =>boolean>(() =>true);// throwsassertIsFunction(async () =>Promise.resolve(null));// throwsassertIsFunction(function* () {});// throwsassertIsFunction(asyncfunction* () {});// throwsassertIsFunction(MyClass);
// does not throw, value is typed as Map<unknown, unknown>assertIsMap(newMap([['xyz', 'abc']]));// does not throw, value is typed as Map<string, string | number>assertIsMap<string, string>(newMap([ ['abc', 'def'], ['xyz', 100], ]), {keyValidator:isString,valueValidator:isUnion(isString, isNumber), },);
assertIsObject(input: unknown, options?: ErrorMessage): asserts input is object
remarks
Tests true for all objects that have a typeof 'object' excluding null
example
// does not throw, value is typed as objectassertIsObject({});// does not throw, value is typed as objectassertIsObject([]);// throwsassertIsObject(null);
// does not throw, value is typed as Record<string | symbol, any>assertIsRecord({ key1:'aaa', key2:123 });// does not throw, value is typed as Record<string, string | number>assertIsRecord<string, string | number>( { key1:'aaa', key2:123 }, {keyValidator:isString,valueValidator:isUnion(isString, isNumber), },);
throws
TypeError
Parameters
input: unknown
Returns asserts input is Record<string | symbol, unknown>
assertIsSet(input: unknown): asserts input is Set<any>
assertIsSet(input: unknown, options: ErrorMessage): asserts input is Set<any>
assertIsSet<T>(input: unknown, options: ValueValidator): asserts input is Set<T>
example
// doesn't throw, value is typed as Set<any>assertIsSet(newSet(['xyz']));// doesn't throw, value is typed as Set<string>assertIsSet<string>(newSet(['xyz']), { valueValidator:isString });
isBuffer(input: unknown, options?: undefined): input is Buffer
Parameters
input: unknown
Optional options: undefined
Returns input is Buffer
Const isDataView
isDataView(input: unknown, options?: undefined): input is DataView
Parameters
input: unknown
Optional options: undefined
Returns input is DataView
Const isDate
isDate(input: unknown, options?: undefined): input is Date
Parameters
input: unknown
Optional options: undefined
Returns input is Date
isDefined
isDefined<T>(input: T | undefined): input is T
Type parameters
T
Parameters
input: T | undefined
Returns input is T
isError
isError<T>(input: unknown): input is T
example
// trueisError(newError());// true, value is typed as ErrorisError(newTypeError());// true, value is typed as TypeErrorisError<TypeError>(newTypeError());// true, as long as MyCustomError inherits ErrorisError<MyCustomError>(newMyCustomError());
Type parameters
T: Error = Error
Parameters
input: unknown
Returns input is T
Const isFloat32Array
isFloat32Array(input: unknown): input is Float32Array
Parameters
input: unknown
Returns input is Float32Array
Const isFloat64Array
isFloat64Array(input: unknown): input is Float64Array
Parameters
input: unknown
Returns input is Float64Array
isFunction
isFunction<T>(input: unknown): input is T
remarks
This guard works only in ES2018 and above
example
// true, value is typed as FunctionisFunction(() =>null);// true, value is typed as () => nullisFunction<() =>null>(() =>null);// falseisFunction(async () =>Promise.resolve(null));// falseisFunction(function* () {});// falseisFunction(asyncfunction* () {});// falseisFunction(MyClass);
Type parameters
T: Function = Function
Parameters
input: unknown
Returns input is T
isGenerator
isGenerator<Y, R, N>(input: unknown): input is Generator<Y, R, N>
// true, value is typed as Map<unknown, unknown>isMap(newMap([['xyz', 'abc']]));// true, value is typed as Map<string, string | number>isMap<string, string>(newMap([ ['abc', 'def'], ['xyz', 100], ]), {keyValidator:isString,valueValidator:isUnion(isString, isNumber), },);
isSet<T>(input: unknown, options: ValueValidator): input is Set<T>
example
// true, value is typed as Set<any>isSet(newSet(['xyz']));// true, value is typed as Set<string>isSet<string>(newSet(['xyz']), { valueValidator:isString });
TypeError