类型检测
Vitarx 提供了一组 isXxx 类型检测函数,每个函数都有 TypeScript 类型守卫,使用后可以自动收窄类型。
isObject
判断值是否为对象(typeof === 'object' 且不为 null)。数组、Map、Set 等也会返回 true。
ts
declare function isObject(val: any): val is { [key: PropertyKey]: any }| 参数 | 类型 | 说明 |
|---|---|---|
val | any | 要判断的变量 |
返回值:boolean
tsx
import { isObject } from 'vitarx'
isObject({}) // true
isObject([]) // true
isObject(null) // false
isObject(42) // falseisPlainObject
判断值是否为纯键值对对象([object Object])。数组、Map、Set 等返回 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) // falseisArray
判断值是否为数组。
ts
declare function isArray(val: any): val is Array<any>tsx
import { isArray } from 'vitarx'
isArray([]) // true
isArray(new Array()) // true
isArray({}) // falseisString
判断值是否为字符串。
ts
declare function isString(val: any): val is stringtsx
import { isString } from 'vitarx'
isString('hello') // true
isString(String(42)) // true
isString(42) // falseisNumber
判断值是否为数字。NaN 和 Infinity 也返回 true。
ts
declare function isNumber(val: any): val is numbertsx
import { isNumber } from 'vitarx'
isNumber(42) // true
isNumber(3.14) // true
isNumber(NaN) // true
isNumber('42') // falseisBool
判断值是否为布尔值。
ts
declare function isBool(val: any): val is booleantsx
import { isBool } from 'vitarx'
isBool(true) // true
isBool(false) // true
isBool(1) // false
isBool('true') // falseisFunction
判断值是否为函数。包括普通函数、箭头函数、async 函数、生成器函数和类。
ts
declare function isFunction(val: any): val is (...args: any[]) => anytsx
import { isFunction } from 'vitarx'
isFunction(() => {}) // true
isFunction(async () => {}) // true
isFunction(class Foo {}) // true
isFunction({}) // falseisAsyncFunction
判断函数是否使用 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()) // falseisPromise
判断值是否为 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: () => {} }) // falseisEmpty
判断值是否为空。以下情况视为空:null、undefined、0、false、''、[]、{}、空 Map、空 Set。
ts
declare function isEmpty(val: any): booleantsx
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) // falseisNumString
判断值是否为纯数字字符串(仅整数)。
ts
declare function isNumString(str: any, allowSpace?: boolean): str is `#123;number}`| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
str | any | — | 要检测的值 |
allowSpace | boolean | false | 是否允许字符串中包含空格 |
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
分别判断值是否为 Map、Set、WeakMap、WeakSet 实例。
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([]) // falseisCollection
判断值是否为集合对象(Map、Set、WeakMap、WeakSet 之一)。
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([]) // falseisDeepEqual
深度比较两个变量内容是否一致,支持指定比较深度。
ts
declare function isDeepEqual(var1: any, var2: any, depth?: number): boolean| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
var1 | any | — | 第一个变量 |
var2 | any | — | 第二个变量 |
depth | number | Infinity | 比较深度,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| 参数 | 类型 | 说明 |
|---|---|---|
target | object | 目标对象 |
key | PropertyKey | 属性键 |
tsx
import { hasOwnProperty } from 'vitarx'
const obj = { name: 'Vitarx' }
hasOwnProperty(obj, 'name') // true
hasOwnProperty(obj, 'toString') // false(原型链上的属性)下一步
- 防抖与节流 — 了解 debounce 和 throttle 工具函数