refactor: 将Data到ViewModel的转换由App转移至Server端
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m15s
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m15s
- 将逻辑转移到Server端后,简化前端逻辑
This commit is contained in:
30
server/utils/object.ts
Normal file
30
server/utils/object.ts
Normal file
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* 判断某一值是否为非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>);
|
||||
};
|
||||
Reference in New Issue
Block a user