一个简单的类型判断工具
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');
}
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,且当是脱裤子放屁吧
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<Person>(value, () => (value && value.name === 'earthnut' && value.age === 18))) {
console.log('value is Person');
return;
}
/// as tisType
if (isType<string>(value)) {
value.toLocaleUpperCase();
return;
}
/// use judgment
if (isType<boolean>(value, Boolean(value) === true )) {
console.log('value is true');
return;
}
}
-
isString
字符串、String
对象构建的字符串 -
isNumber
数字、Number
对象构建的数字 -
isBoolean
布尔值、Boolean
对象构建的布尔值 -
isNull
null -
isUndefined
undefined -
isNaN
NaN
(NaN
是一个特殊的数值NaN !== NaN
即便typeof NaN
返回的是number
) -
isFunction
函数 -
isArray
数组、Array
对象构建的数组 -
isEmptyString
空字符串 -
isBusinessEmptyString
空字符串(去除首尾空格) -
isSymbol
symbol -
isBigInt
大整数 -
isPlainObject
对象(普通对象,非其他内置对象类型) -
isPromise
Promise -
isAsyncFunction
异步函数 -
isDate
时间 -
isMap
Map -
isSet
Set -
isWeakMap
WeakMap -
isWeakSet
WeakSet -
isGenerator
生成器 -
isGeneratorFunction
生成器函数 -
isBigInt64Array
BigInt64Array -
isBigUint64Array
BigUint64Array -
isDataView
DataView -
isArrayBuffer
ArrayBuffer -
isRegExp
正则、RegExp
对象构建的正则 -
isSharedArrayBuffer
SharedArrayBuffer -
isUint8ClampedArray
Uint8ClampedArray -
isInt8Array
Int8Array -
isUint8Array
Uint8Array -
isTypedArray
TypedArray -
isInt16Array
Int16Array -
isUint16Array
Uint16Array -
isInt32Array
Int32Array -
isUint32Array
Uint32Array -
isFloat32Array
Float32Array -
isFloat64Array
Float64Array -
isIntlCollator
Intl.Collator -
isIntlDateTimeFormat
Intl.DateTimeFormat -
isIntlDisplayNames
Intl.DisplayNames -
isIntlListFormat
Intl.ListFormat -
isIntlLocale
Intl.Locale -
isIntlNumberFormat
Intl.NumberFormat -
isError
错误 -
isEvalError
eval 错误 -
isRangeError
range 错误 -
isReferenceError
reference 错误 -
isSyntaxError
syntax 错误 -
isTypeError
type 错误 -
isURIError
uri 错误 -
isAggregateError
aggregate 错误