Last active
July 17, 2020 03:38
-
-
Save eightHundreds/a0e89e55f70439b0eaaa2bc67fab2c9a to your computer and use it in GitHub Desktop.
Ts工具类型声明
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Api { | |
type PageParam<T = {}> = T & { pageNum?: number; pageSize?: number }; | |
type ApiResult<T = {}> = { | |
[key: string]: any; // 其他基本用不到,想知道看文档 | |
/** | |
* 调用是否成功 | |
*/ | |
success: boolean; | |
/** | |
* 业务状态码 | |
*/ | |
code: number; | |
/** | |
* 状态码描述 | |
*/ | |
message: string; | |
data?: T; | |
}; | |
type PageResult<T> = { | |
total: number; | |
list: T[]; | |
/** | |
* 当前页,从1开始计算 | |
*/ | |
pageNum: number; | |
/** | |
* 每页最大行数 | |
*/ | |
pageSize: number; | |
/** | |
* 返回数据列表长度 | |
*/ | |
size: number; | |
startRow: number; | |
endRow: number; | |
/** | |
* 总页数 | |
*/ | |
pages: number; | |
}; | |
/** | |
* 获得Api方法结果的类型 | |
* @example | |
* export function getUser () { | |
* return axios.get<Model.User>(`/api/coral/userProfile/find`); | |
* } | |
* | |
* type theResult = ResultOfApi<typeof getUser>; // Model.User | |
*/ | |
type ResultOfApi<T> = T extends (...args: any[]) => Promise<Api.ApiResult<infer P>> ? P : never; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment