Files
jinshen-website/app/models/utils/object.ts
R2m1liA 98f978484c feat: directus视图与转换函数
- views: 用于前端渲染的视图模型
- mapper: 用于视图模型转换的转换函数
- utils: 相关工具函数
2025-10-15 16:48:38 +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>);
};