Typecheck and narrow down the variables/parameters with typeguard
import { isString } from 'typesafe-is';
let x: string | number;
if (isString(x)) {
console.log(x.subtr(1)); // Error, 'subtr' does not exist on `string`
console.log(x.substr(1)); // OK
} else {
console.log(x.substr(1)); // Error
console.log(x.subtr(1)); // OK, 'subtr' exists on number
}
This module is distributed via npm which is bundled with node and
should be installed as one of your project's dependencies
:
npm install --save typesafe-is
or
yarn add typesafe-is
All functions can be imported with one of the following statements:
import { isNil } from 'typesafe-is'; // this works
const { isNil } = require('typesafe-is'); // this works too
See here for full list of available typechecking functions.
Generated using TypeDoc