ts-type-guards
Curried TypeScript type guards for primitive types and classes
Simplifies typechecking by providing type guards to check if something is of a certain type or of the same type as something else. Includes tailor-made type guards for the primitive types and a general one for "classy" types.
Installation
npm install ts-type-guards --save
Usage Examples
Basic Usage
; const header = document;console; // Error: Object is possibly 'null'.if header console; // Compiles and runs safely.
Because is
, only
etc are curried, you can use them like so:
; const foos = Array;const fooImages = foos;const srcs = fooImages; // Compiles and runs safely.
Equivalent:
; const foos = Array;const fooImages = foos;const srcs = fooImages; // Compiles and runs safely.
Checking Against Another Value
Use isLike
to check if something is of the same type as a reference value:
; // We want to make sure that this function always returns a T:: T { const saved: string | null = localStorage; if return fallback; const parsed: any = JSON; return parsed ? parsed // parsed is like fallback, so it is a T! : fallback // parsed has wrong type, so return fallback. ;} ; // Guaranteed to be a number.
(Note that this function can still throw DOMException
or SyntaxError
, but that's not a typechecking problem.)
Subclasses
is
is basically a partially applicable instanceof
. For classy types, isLike(ref)(x)
is equivalent to x instanceof ref.constructor
.
{} {} {} const someone = ;const simba = ;const nala = ;const pumbaa = ; simba; // truesimba; // truesimba; // falsesomeone; // false simba; // truesimba; // truesimba; // falsesomeone; // false
Primitive Types
is
can only handle classy types, so the primitive ones have their own type guards:
; // true; // false
isLike
supports the primitive types as well:
10; // true (because all numbers are floating point in JS)undefined; // false
The non-primitive types Boolean
, Number
and String
share some, but not all, semantics with the primitive types boolean
, number
and string
, respectively. The main difference lies in their equality semantics:
"foo" === "foo" ; // true"foo" === "foo"; // false
ts-type-guards
includes type guards for the cases when you don't care whether a value is of a primitive type or its pseudo-primitive counterpart. For example, to check if a value is either a string
or a String
, use isStringLike
.
Reusing Type Guards
Although it may seem clunky to have to write is(x)(y)
instead of is(x, y)
, this is a design choice based on the fact that partial application is so awesome. Not only does it get rid of xs.filter(x => is(T, x))
in favor of xs.filter(is(T))
, it also lets you save and reuse type guards:
const isFoo = ; if x; xs;
Arrays
You can check if something is an array of a certain type:
; // true; // false ; // true
Contributing
- Fork the repo.
- Create your feature branch (
git checkout -b feature/foobar
). - Examine and add your changes (
git diff
, thengit add ...
). - Commit your changes (
git commit -m 'Add some foobar'
). - Push your feature branch (
git push origin feature/foobar
). - Create a pull request.