Skip to content

kgtkr/safe-type-predicate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

safe-type-predicate

Build Status

Install

$ npm i safe-type-predicate

Usage

import { isT, isNotT, defineIsT } from "safe-type-predicate";

// isString: (x: unknown) => x is string
const isString = defineIsT((x: unknown) =>
    typeof x === "string" ? isT(x) : isNotT()
);

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

// isA: (x: "a" | "b") => x is "a"
const isA = defineIsT((x: "a" | "b") =>
    x === "a" ? isT(x) : isNotT()
);

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

Linting

Be more safe by using tslint custom rules.

install

$ npm i tslint-safe-type-predicate

add tslint.json

// tslint.json
{
    "extends": [
        "tslint-safe-type-predicate",
        // ...
    ],
    // ...
}

linting

defineIsT((x: T) => (e) ? isT(y) : isNotT());
                          ~~~~~~ [expect: 'isT(x)']

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published