A powerful typescript
library that helps you build your type guards.
The library utilizes the typescript
compiler to ensure the type guards are type safe and fast to create.
- TypeGuard
- Guarded
- isType
- isTuple
- isUnion
- isIntersection
- isArray
- isInstanceof
- isOptional
- isMaybe
- isString
- isStringArray
- isNumber
- isDate
- isNull
- isUndefined
- isNil - Utility type for checking for null or undefined
The most basic type - represents a type guard of T
type TypeGuard<T> = (value: unknown) => value is T;
Extracts T
out of TypeGuard<T>
type Test = Guarded<TypeGuard<number>>; // number
Helps you create type guards for types and interfaces
type Test = {
a: number;
b: string;
};
const isTest = isType<Test>({
a: isNumber,
b: isString,
});
isTest({ a: 6, b: "Hello" }) // true
isType
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 === isTree
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);
Helps you create type guards for tuples
type Record = [number, string?];
const isRecord = isTuple<Record>([isNumber, isOptionalString]);
isRecord([6, "Hello"]) // true
isRecord([6]) // true
isRecord(["Hello", "Bye"]) // false
Just like isType
, isTuple
supports recursive tuples
type Record = [number, Record | null];
const isRecord = isTuple<Record>(() => [
isNumber,
isMaybe(isRecord),
]);
// isRecord can also be accessed via the function's parameter
const isRecord2 = isTuple<Record>(isRecordParam => [
isNumber,
isMaybe(isRecordParam), // isRecordParam === isRecord2
]);
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
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);
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);
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);
Helps you create type guards for optional types
const isNumberOrUndefined: TypeGuard<number | undefined> = isOptional(isNumber);
Helps you create type guards for nullable types
const isNumberOrNull: TypeGuard<number | null> = isMaybe(isNumber);