Promise的泛型T(Promise<T>)的含义

interface AxiosInstance {
  <T = any>(value: T): Promise<T>
}

Promise的泛型T代表promise变成成功态之后resolve的值,resolve(value)

先看看泛型接口来定义普通函数

interface CreateArrayFunc {
  <T>(length: number, value: T): Array<T>
}

let createArray: CreateArrayFunc;
createArray = function <T>(length: number, value: T): Array<T> {
  let result: T[] = [];
  for (let i = 0; i < length; i++) {
    result[i] = value
  }
  return result
}

createArray(3, 'x')  // ['x', 'x', 'x']

同理

interface AxiosInstance {
  <T = any>(value: T): Promise<T>
}

let instance: AxiosInstance;
instance = function <T>(value: T): Promise<T> {
  return new Promise((resolve) => {
    resolve(value)
  })
}
instance(100)
相关推荐
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页