JavaScript/TypeScript 的类型检测工具,支持 TypeScript 类型收缩
npm install 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
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
`
en,且当是脱裤子放屁吧
`ts
import { isType } from 'a-type-of-js';
interface Person {
name: string;
age: number;
}
function doSomething(value: string | number | boolean | ) {
/// use judgment as a type judgment
if (isType
console.log('value is Person');
return;
}
/// as tisType
if (isType
value.toLocaleUpperCase();
return;
}
/// use judgment
if (isType
console.log('value is true');
return;
}
}
`
- isString 字符串、String 对象构建的字符串isEmptyString
- 空字符串isBusinessEmptyString
- 空字符串(去除首尾空格)isNumber
- 数字、Number 对象构建的数字isBoolean
- 布尔值、Boolean 对象构建的布尔值isTrue
- 是否为 trueisFalse
- 是否为 falseisNull
- nullisUndefined
- undefinedisNaN
- NaN(NaN 是一个特殊的数值 NaN !== NaN 即便 typeof NaN 返回的是 number)isFunction
- 函数isArray
- 数组、Array 对象构建的数组isEmptyArray
- 是否是空数组isSymbol
- symbolisBigInt
- 大整数isPlainObject
- 对象(普通对象,非其他内置对象类型)isEmptyObject
- 是否为空对象(没有私有键的对象)isPromise
- PromiseisAsyncFunction
- 异步函数isDate
- 时间isMap
- MapisSet
- SetisWeakMap
- WeakMapisWeakSet
- WeakSetisGenerator
- 生成器isGeneratorFunction
- 生成器函数isBigInt64Array
- BigInt64ArrayisBigUint64Array
- BigUint64ArrayisDataView
- DataViewisArrayBuffer
- ArrayBufferisRegExp
- 正则、RegExp 对象构建的正则isSharedArrayBuffer
- SharedArrayBufferisUint8ClampedArray
- Uint8ClampedArrayisInt8Array
- Int8ArrayisUint8Array
- Uint8ArrayisTypedArray
- TypedArrayisInt16Array
- Int16ArrayisUint16Array
- Uint16ArrayisInt32Array
- Int32ArrayisUint32Array
- Uint32ArrayisFloat32Array
- Float32ArrayisFloat64Array
- Float64ArrayisIntlCollator
- Intl.CollatorisIntlDateTimeFormat
- Intl.DateTimeFormatisIntlDisplayNames
- Intl.DisplayNamesisIntlListFormat
- Intl.ListFormatisIntlLocale
- Intl.LocaleisIntlNumberFormat
- Intl.NumberFormatisError
- 错误isEvalError
- eval 错误isRangeError
- range 错误isReferenceError
- reference 错误isSyntaxError
- syntax 错误isTypeError
- type 错误isURIError
- uri 错误isAggregateError` aggregate 错误
-