npm.io
1.0.0 • Published 7 years ago

safe-is-type-ts

Licence
MIT
Version
1.0.0
Deps
0
Size
11 kB
Vulns
0
Weekly
0
Stars
15

safe-is-type-ts

Build Status

Install

$ npm i safe-is-type-ts

Usage

import { isType, isNotType, toIsTypeFunction } from "safe-is-type-ts";

// isString: (x: unknown) => x is string
const isString = toIsTypeFunction((x: unknown) =>
    typeof x === "string" ? isType(x) : isNotType()
);

isString("x"); // true
isString(null); // false

// isA: (x: "a" | "b") => x is "a"
const isA = toIsTypeFunction((x: "a" | "b") =>
    x === "a" ? isType(x) : isNotType()
);

isA("a"); // true
isA("b"); // false
isA("c"); // Argument of type '"c"' is not assignable to parameter of type '"a" | "b"'