Files
jinshen-website/server/utils/object.ts
R2m1liA 23f2700c0f
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m15s
refactor: 将Data到ViewModel的转换由App转移至Server端
- 将逻辑转移到Server端后,简化前端逻辑
2025-11-13 20:45:43 +08:00

31 lines
998 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 判断某一值是否为非null对象
*
* @template T 泛型类型,用于推断目标对象的类型
* @param value: 需要判断的值
* @returns 如果值是非null对象则返回true否则返回false
*
* @example
* if (isObject<Product>(value)) value.id
*/
export const isObject = <T extends object>(value: unknown): value is T =>
typeof value === 'object' && value !== null;
/**
* 判断某一值是否为非null对象组成的数组
*
* @template T 泛型类型,用于推断目标对象的类型
* @param value: 需要判断的值
* @returns 如果值是非null对象组成的数组则返回true否则返回false
*
* @example
* const data: unknown = [{ id: 1 }, { id: 2 }];
* if (isArrayOfObject)<{ id: number }>(data) {
* // TypeScript 知道 data 是 { id: number }[] 类型
* console.log(data[0].id);
* }
*/
export const isArrayOfObject = <T extends object>(arr: unknown): arr is T[] => {
return Array.isArray(arr) && arr.every(isObject<T>);
};