🀢

a-type-of-js

versionNPM Last Update📦 sizeMonthly downloadsTotal downloads


GitHub last commitGitHub commit activityCoverage StatusCodecov


一个简单的类型判断工具

安装

✂️
bash
npm install --save a-type-of-js

使用

✂️
ts
import { typeOf } from 'a-type-of-js';

const num = 1;

if (typeOf(num) === 'number') {
  console.log('num is number');
} else {
  console.log('num is not number');
}

使用 ts 中的 is 类型判断来进行类型安全收缩

✂️
ts
import { isString, isNumber } from 'a-type-of-js';

function doSomething(value: string | number) {
  if (isString(value)) {
    value.toLocaleUpperCase();
  } else if (isNumber(value)) {
    value.toFixed(2);
  }
}

doSomething('hello'); // HELLO
doSomething(1); // 1.00