Skip to content

Instantly share code, notes, and snippets.

@eightHundreds
Last active July 17, 2020 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eightHundreds/a0e89e55f70439b0eaaa2bc67fab2c9a to your computer and use it in GitHub Desktop.
Save eightHundreds/a0e89e55f70439b0eaaa2bc67fab2c9a to your computer and use it in GitHub Desktop.
Ts工具类型声明
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