Package Exports
- isguard-ts
- isguard-ts/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (isguard-ts) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
isguard-ts
A powerful typescript library that helps you build type guards.
isguard-ts utilizes the typescript compiler to ensure the type guards are type safe and fast to create.
Some of our built-in types
Some of our built-in helper functions
- isType
- isTuple
- isUnion
- isIntersection
- isArray
- isEnum
- isSet
- isMap
- isRecord
- isIndexRecord
- isInstanceof
- isOptional
- isMaybe
Some of our utility type guards
- isString
- isStringArray
- isNumber
- isDate
- isNull
- isUndefined
- isNil -
nullorundefined - isUnknown - always returns
true - isNever - always returns
false
Code Examples
TypeGuard<T>
The most basic type - represents a type guard of T
type TypeGuard<T> = (value: unknown) => value is T;
Guarded<T>
Extracts T out of TypeGuard<T>
type Test = Guarded<TypeGuard<number>>; // number
isType<T>(template): TypeGuard<T>
Helps you create type guards for types and interfaces
type Person = {
name: string;
age: number;
};
const isPerson = isType<Person>({
name: isString,
age: isNumber,
});
isPerson({ name: "Hello", age: 6 }) // trueisType also supports recursive types by passing a function as an argument
type Tree = {
value: number;
left: Tree | null;
right: Tree | null;
};
const isTree = isType<Tree>(() => ({
value: isNumber,
left: isMaybe(isTree), // isTree is the return type of isType
right: isMaybe(isTree),
}));
// isTree can also be accessed via the passed function's parameter
const isTree2 = isType<Tree>(isTreeParam => ({
value: isNumber,
left: isMaybe(isTreeParam), // isTreeParam === isTree2
right: isMaybe(isTreeParam),
}));For generic types you would need to create your own type guard generator
type ValueHolder<T> = {
value: T;
};
const isValueHolder = <T>(isValue: TypeGuard<T>): TypeGuard<ValueHolder<T>> => {
return isType<ValueHolder<T>>({
value: isValue,
});
};
const isNumberHolder: TypeGuard<ValueHolder<number>> = isValueHolder(isNumber);
isTuple<T>(template): TypeGuard<T>
Helps you create type guards for tuples
type Row = [number, string?];
const isRow = isTuple<Row>([isNumber, isOptionalString]);
isRow([6, "Hello"]) // true
isRow([6]) // true
isRow(["Hello", "Bye"]) // falseJust like isType, isTuple supports recursive tuples
type Row = [number, Row | null];
const isRow = isTuple<Row>(() => [
isNumber,
isMaybe(isRow),
]);
// isRow can also be accessed via the function's parameter
const isRow = isTuple<Row>(isRowParam => [
isNumber,
isMaybe(isRowParam), // isRowParam === isRow2
]);
isUnion<[T1, T2, ...]>(...guards): TypeGuard<T1 | T2 | ...>
Helps you create type guards for unions
const isNumberOrString: TypeGuard<number | string> = isUnion(isNumber, isString);
isNumberOrString(6) // true
isNumberOrString("Hello") // true
isNumberOrString(new Date()) // false
isIntersection<[T1, T2, ...]>(...guards): TypeGuard<T1 & T2 & ...>
Helps you create type guards for intersections
type A = { a: number };
type B = { b: string };
type C = A & B;
const isA = isType<A>({ a: isNumber });
const isB = isType<B>({ b: isString });
const isC: TypeGuard<C> = isIntersection(isA, isB);
isArray<T>(guard: TypeGuard<T>): TypeGuard<T[]>
Helps you create type guards for arrays
const isNumberArray = isArray(isNumber);
type Test = { a: number };
const isTest = isType<Test>({ a: isNumber });
const isTestArray: TypeGuard<Test[]> = isArray(isTest);
isEnum<T>(enumObj: T): TypeGuard<T[keyof T]>
Helps you create type guards for enums
enum Direction {
up = 0,
down = 1,
left = 2,
right = 3,
}
const isDirection: TypeGuard<Direction> = isEnum(Direction);
isDirection(Direction.up) // true
isDirection(2) // true
isDirection("hello") // false
isSet<T>(guard: TypeGuard<T>): TypeGuard<Set<T>>
Helps you create type guards for sets
const isNumberSet = isSet(isNumber);
isMap<K, V>(isKey: TypeGuard<K>, isValue: TypeGuard<V>): TypeGuard<Map<K, V>>
Helps you create type guards for maps
const isStringBooleanMap = isMap(isString, isBoolean);
isRecord<K, V>(isKey: TypeGuard<K>, isValue: TypeGuard<V>): TypeGuard<Record<K, V>>
Helps you create type guards for records
type TimeUnit = "second" | "minute" | "hour";
type TimeUnitToMillisecond = Record<TimeUnit, number>;
const isTimeUnit: TypeGuard<TimeUnit> = isValueUnion("second", "minute", "hour");
const isTimeUnitToMillisecond = isRecord(isTimeUnit, isNumber);
isIndexRecord<V>(isValue: TypeGuard<V>): TypeGuard<Record<PropertyKey, V>>
Works just like isRecord but checks only the values and not the keys
const isNumberRecord: TypeGuard<Record<PropertyKey, number>> = isIndexRecord(isNumber);
isInstanceof<T>(constructor): TypeGuard<T>
Helps you create type guards for classes
abstract class Animal { }
class Dog extends Animal { }
const isAnimal: TypeGuard<Animal> = isInstanceof(Animal);
const isDog: TypeGuard<Dog> = isInstanceof(Dog);
isOptional<T>(guard: TypeGuard<T>): TypeGuard<T | undefined>
Helps you create type guards for optional types
const isNumberOrUndefined: TypeGuard<number | undefined> = isOptional(isNumber);
isMaybe<T>(guard: TypeGuard<T>): TypeGuard<T | null>
Helps you create type guards for nullable types
const isNumberOrNull: TypeGuard<number | null> = isMaybe(isNumber);