-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.d.ts
More file actions
102 lines (84 loc) · 2.86 KB
/
types.d.ts
File metadata and controls
102 lines (84 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
export interface CallbackFn {
(state?: any): void;
}
export interface GetStateFn<T> {
(): T;
}
export interface SetStateFn<T> {
(newState: Partial<T>, callback?: CallbackFn): void;
}
export interface SetSingleStateFn<T> {
(partialStates: Partial<T>, callback?: CallbackFn): void;
}
export type KeyOf<T> = Extract<keyof T, string>;
export type CancelDebounceFn = () => void;
export type IntervalClear = () => void;
export type IntervalFn = (
fn: Function,
delay: number,
options: {
immediate?: boolean;
}
) => () => void;
export type IntervalHook = readonly [IntervalFn, IntervalClear];
export type FormField<T, D extends FormDataType> = {
value: T;
required?: boolean;
validator?: (value: T, data: D) => string | null | undefined;
message?: string;
};
export type FormDataType = {
[key: string]: any;
};
export type FormSubmitResult<T extends FormDataType> =
| T
| {[K in keyof T]?: string};
export type UseFormChangeResult<
T extends Record<string, FormField<any, FormDataType>>,
> = [
FormDataType,
(formItem: Partial<FormDataType>) => void,
{[K in keyof T]?: string},
() => Promise<FormSubmitResult<FormDataType>>,
() => void,
];
export type DebounceOptions = {
delay: number; // 防抖间隔(毫秒)
leading?: boolean; // 是否在防抖开始时立即执行(默认 true)
trailing?: boolean; // 是否在防抖结束后执行最后一次调用(默认 true)
};
export type DebounceFn<T extends (...args: any[]) => any> = {
execute: (...args: Parameters<T>) => void; // 【必选】包装后的防抖函数
flush: () => void;
cancel: () => void; // 【必选】取消待执行的防抖调用
isPending: boolean; // 【推荐】当前是否有待处理的防抖调用
};
export type ThrottleOptions = {
delay: number; // 节流间隔(毫秒)
leading?: boolean; // 是否在节流开始时立即执行(默认 true)
trailing?: boolean; // 是否在节流结束后执行最后一次调用(默认 true)
};
export type ThrottledFn<T extends (...args: any[]) => any> = {
execute: (...args: Parameters<T>) => void; // 【必选】包装后的节流函数
cancel: () => void; // 【必选】取消待执行的节流调用
flush: () => void; // 【推荐】立即执行最后一次调用
isPending: boolean; // 【推荐】当前是否有待处理的节流调用
};
export type MockRequestConfig<T = any> = {
delay?: number; // 延迟时间(默认 200-500ms 随机)
successRate?: number; // 成功概率(0-1,默认 0.8)
mockData?: T; // 自定义返回数据
};
export type RequestState<T> = {
loading: boolean;
error: Error | null;
data: T | null;
};
export type ApiResponse<T> = {
status: number;
data: T;
};
export interface UseMockRequestReturn<T> extends RequestState<ApiResponse<T>> {
mockRequest: (config?: MockRequestConfig<T>) => Promise<ApiResponse<T>>;
cancelRequest: () => void;
}