loading

TS - 扩展模块类型时要在顶级处添加 export {}

如果在 .d.ts 文件中扩展 Element、HTMLElement 这种内置的全局类型接口时,直接写就可以了。

file:[types/shim-elems.d.ts]
declare interface Element {
  innerText?: string;
}

如果扩展的是第三方模块,比如 axios。还是按照上面这样的做法是不对的,这相当于在全局声明了一个 AxiosInstance,并没有扩展到第三方模块里面的 AxiosInstance 类型。

file:[types/shim-axios.d.ts]
declare module "axios" {
  interface AxiosInstance {
    ejectReq: () => AxiosInstance;
    ejectRes: () => AxiosInstance;
  }
}

所以,对于上述情况,我们需要在顶处添加一个 export {} 或者 import 语句,export {} 确保该文件不会覆盖 axios 模块下原来的类型。

file:[types/shim-axios.d.ts]
export {};

declare module "axios" {
  interface AxiosInstance {
    ejectReq: () => AxiosInstance;
    ejectRes: () => AxiosInstance;
  }
}
posted @ 2023-05-13 00:02  Himmelbleu  阅读(10)  评论(0编辑  收藏  举报