🀢

a-type-of-js

version NPM Last Update 📦 size downloads downloads

last commit GitHub commit activity


中文 🇨🇳 English 🌍 查看 📔 日志 bug 🙋‍♂️ 提交


versionNPM Last Update📦 sizedownloadsdownloads


last commitGitHub commit activity


中文 🇨🇳English 🌍查看 📔 日志bug 🙋‍♂️ 提交

一个简单的类型判断工具

安装

✂️
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