umijs / umi-request

A request tool based on fetch.
2.2k stars 336 forks source link

请求response返回data , 如何声明类型呢 ? #195

Closed xoptimal closed 3 years ago

xoptimal commented 3 years ago

下面这个T , 我该如何声明 ???

export interface RequestMethod<R = false> {
  <T = any>(url: string, options: RequestOptionsWithResponse): Promise<RequestResponse<T>>;
  <T = any>(url: string, options: RequestOptionsWithoutResponse): Promise<T>;
  <T = any>(url: string, options?: RequestOptionsInit): R extends true ? Promise<RequestResponse<T>> : Promise<T>;
  ....
}

// eg.  这里不知道怎么传递这个 T
const res = await request(url, {
  method: method,
  params: body,
});
xoptimal commented 3 years ago

@yesmeck help me, please~

yesmeck commented 3 years ago
interface Data {
...
}

const res = await request<Data>(url, {
  method: method,
  params: body,
});
xoptimal commented 3 years ago

抱歉, 这个request 不是纯粹的 umi-request , 经过了一层 extend ~

const request = extend({
  errorHandler, // 默认错误处理
  timeout: 10 * 1000,
  getResponse: true,
  // parseResponse: false,
  // credentials: 'include', // 默认请求是否带上cookie
});

以上这样调用的, 查看源码后, 是这样的:

export interface Extend {
  (options: ExtendOptionsWithoutResponse): RequestMethod<false>;
  (options: ExtendOptionsWithResponse): RequestMethod<true>;
  (options: ExtendOptionsInit): RequestMethod;
}

请问怎么保留之前的 reqeust 形式呢~ @yesmeck

yesmeck commented 3 years ago

应该是一样的用法。