类型检测

Vitarx 提供了一组 isXxx 类型检测函数,每个函数都有 TypeScript 类型守卫,使用后可以自动收窄类型。

isObject

判断值是否为对象(typeof === 'object' 且不为 null)。数组、MapSet 等也会返回 true

ts
declare function isObject(val: any): val is { [key: PropertyKey]: any }
参数类型说明
valany要判断的变量

返回值boolean

tsx
import { isObject } from 'vitarx'

isObject({}) // true
isObject([]) // true
isObject(null) // false
isObject(42) // false

isPlainObject

判断值是否为纯键值对对象([object Object])。数组、MapSet 等返回 false

ts
declare function isPlainObject(val: any): val is Record<string, any>
tsx
import { isPlainObject } from 'vitarx'

isPlainObject({}) // true
isPlainObject([]) // false
isPlainObject(new Map()) // false
isPlainObject(null) // false

isArray

判断值是否为数组。

ts
declare function isArray(val: any): val is Array<any>
tsx
import { isArray } from 'vitarx'

isArray([]) // true
isArray(new Array()) // true
isArray({}) // false

isString

判断值是否为字符串。

ts
declare function isString(val: any): val is string
tsx
import { isString } from 'vitarx'

isString('hello') // true
isString(String(42)) // true
isString(42) // false

isNumber

判断值是否为数字。NaNInfinity 也返回 true

ts
declare function isNumber(val: any): val is number
tsx
import { isNumber } from 'vitarx'

isNumber(42) // true
isNumber(3.14) // true
isNumber(NaN) // true
isNumber('42') // false

isBool

判断值是否为布尔值。

ts
declare function isBool(val: any): val is boolean
tsx
import { isBool } from 'vitarx'

isBool(true) // true
isBool(false) // true
isBool(1) // false
isBool('true') // false

isFunction

判断值是否为函数。包括普通函数、箭头函数、async 函数、生成器函数和类。

ts
declare function isFunction(val: any): val is (...args: any[]) => any
tsx
import { isFunction } from 'vitarx'

isFunction(() => {}) // true
isFunction(async () => {}) // true
isFunction(class Foo {}) // true
isFunction({}) // false

isAsyncFunction

判断函数是否使用 async 关键字声明。注意:返回 Promise 的普通函数不会返回 true

ts
declare function isAsyncFunction(func: Function): func is (...args: any[]) => Promise<any>
tsx
import { isAsyncFunction } from 'vitarx'

isAsyncFunction(async () => {}) // true
isAsyncFunction(() => {}) // false
isAsyncFunction(() => Promise.resolve()) // false

isPromise

判断值是否为 Promise 实例。

ts
declare function isPromise(val: any): val is Promise<any>
tsx
import { isPromise } from 'vitarx'

isPromise(new Promise(() => {})) // true
isPromise(Promise.resolve()) // true
isPromise({ then: () => {} }) // false

isEmpty

判断值是否为空。以下情况视为空:nullundefined0false''[]{}、空 Map、空 Set

ts
declare function isEmpty(val: any): boolean
tsx
import { isEmpty } from 'vitarx'

isEmpty(null) // true
isEmpty('') // true
isEmpty([]) // true
isEmpty({}) // true
isEmpty(new Map()) // true
isEmpty([1]) // false
isEmpty({ a: 1 }) // false
isEmpty(42) // false
isEmpty(true) // false

isNumString

判断值是否为纯数字字符串(仅整数)。

ts
declare function isNumString(str: any, allowSpace?: boolean): str is `#123;number}`
参数类型默认值说明
strany要检测的值
allowSpacebooleanfalse是否允许字符串中包含空格
tsx
import { isNumString } from 'vitarx'

isNumString('123') // true
isNumString('00123') // true
isNumString('123.45') // false(不支持小数)
isNumString(' 123 ') // false
isNumString(' 123 ', true) // true(允许空格)
isNumString(123) // false(不是字符串)

isMap / isSet / isWeakMap / isWeakSet

分别判断值是否为 MapSetWeakMapWeakSet 实例。

ts
declare function isMap(obj: any): obj is Map<any, any>
declare function isSet(obj: any): obj is Set<any>
declare function isWeakMap(obj: any): obj is WeakMap<WeakKey, any>
declare function isWeakSet(obj: any): obj is WeakSet<WeakKey>
tsx
import { isMap, isSet, isWeakMap, isWeakSet } from 'vitarx'

isMap(new Map()) // true
isSet(new Set()) // true
isWeakMap(new WeakMap()) // true
isWeakSet(new WeakSet()) // true
isMap({}) // false
isSet([]) // false

isCollection

判断值是否为集合对象(MapSetWeakMapWeakSet 之一)。

ts
declare function isCollection(
  obj: any
): obj is Map<any, any> | Set<any> | WeakMap<WeakKey, any> | WeakSet<WeakKey>
tsx
import { isCollection } from 'vitarx'

isCollection(new Map()) // true
isCollection(new Set()) // true
isCollection(new WeakMap()) // true
isCollection({}) // false
isCollection([]) // false

isDeepEqual

深度比较两个变量内容是否一致,支持指定比较深度。

ts
declare function isDeepEqual(var1: any, var2: any, depth?: number): boolean
参数类型默认值说明
var1any第一个变量
var2any第二个变量
depthnumberInfinity比较深度,0 表示只比较引用
tsx
import { isDeepEqual } from 'vitarx'

// 基本比较
isDeepEqual(1, 1) // true
isDeepEqual('hello', 'hello') // true
isDeepEqual(NaN, NaN) // true

// 对象比较
isDeepEqual({ a: 1 }, { a: 1 }) // true
isDeepEqual({ a: 1 }, { a: 2 }) // false

// 限制比较深度
const obj1 = { a: 1, b: { c: { d: 3 } } }
const obj2 = { a: 1, b: { c: { d: 4 } } }
isDeepEqual(obj1, obj2, 1) // true(只比较一层)
isDeepEqual(obj1, obj2, 2) // false(比较两层,发现 b.c.d 不同)

hasOwnProperty

检查对象是否包含指定的自有属性(不检查原型链)。

ts
declare function hasOwnProperty(target: object, key: PropertyKey): boolean
参数类型说明
targetobject目标对象
keyPropertyKey属性键
tsx
import { hasOwnProperty } from 'vitarx'

const obj = { name: 'Vitarx' }
hasOwnProperty(obj, 'name') // true
hasOwnProperty(obj, 'toString') // false(原型链上的属性)

下一步