Skip to content

Latest commit

 

History

History
182 lines (124 loc) · 3.25 KB

TypeGuard.md

File metadata and controls

182 lines (124 loc) · 3.25 KB

TypeGuard

IsPrimitive

isPrimitive(value: unknown): boolean

Narrow down the value to being nullish, a boolean, a number, or a string.

IsDefined

isDefined(value: unknown): boolean

Narrow down the value to being not nullish.

IsBoolean

isBoolean(value: unknown): boolean

Narrow down the value to being a boolean.

IsNumber

isNumber(value: unknown): boolean

Narrow down the value to being a number, but not NaN.

IsInteger

isInteger(value: unknown): boolean

Narrow down the value to being a safe integer.

IsFiniteNumber

isFiniteNumber(value: unknown): boolean

Narrow down the value to being a number, but not NaN nor +/-Infinity.

IsString

isString(value: unknown): boolean

Narrow down the value to being a string.

IsArray

isArray(value: unknown, constraints?: ArrayConstraints): boolean

Narrow down the value to being an array.

The optional parameter constraints accept an object described by the following interface.

interface ArrayConstraints<T>
{
	minLength?: number;
	itemGuard?: (item: unknown) => item is T;
}

If minLength is provided, it'll confirm that the value has at least that many items.
If itemGuard is provided, it'll confirm that the predicate hold true for every item.

IsPopulatedArray

isPopulatedArray(value: unknown, constraints?: ArrayConstraints): boolean

Like IsArray, but narrow it to being a populated array.

IsRecord

isRecord(value: unknown, itemGuard?: <T>(item: unknown) => item is T): boolean

Narrow down the value to being a record: an object with no prototype, or directly using Object prototype.

Symbol keys are ignored when validating record items.

IsObject

isObject(value: unknown): boolean

Narrow down the value to being an object.

IsFunction

isFunction(value: unknown): boolean

Narrow down the value to being a function, generator function, method, or class.

IsCallable

isCallable(value: unknown): boolean

Narrow down the value to being not constructible.## HasAllowedKeys

hasAllowedKeys(value: object, allowed_keys: Array<string>): boolean

Confirm that the value only has allowed keys.

HasNullableProperty

hasNullableProperty(value: object, property: string): boolean

Narrow down the value to being an object with the property defined, though it may be nullish.

HasProperty

hasProperty(value: object, property: string): boolean

Narrow down the value to being an object with the property defined.

IsStructuredData

isStructuredData(value: object, descriptor: TypeGuardStructuredDataDescriptor<T>): boolean

Narrow down the value to a specificly structured data object. The descriptor is enforced into matching the type it narrows down to.

For each possible property, you can specify a boolean flag optional if the property do not need to exists. And a flag nullable if the property value can be nullish.

Example of use

interface ICat
{
	name: string;
	birthTimestamp?: number;
	tagId: string|undefined;
}

isStructuredData(
	value,
	{
		name: {
			test: isString
		},
		birthTimestamp: {
			optional: true,
			test: isNumber
		},
		tagId: {
			nullable: true,
			test: isString
		},
	}
)