需求背景

具体代码

declare module 'axios' {
export interface AxiosRequestConfig {
/**
* @description 设置为true,则会在请求过程中显示loading动画,直到请求结束才消失
*/
loading?: boolean;
}
}

声明合并

举个例子

interface A {
name: string;
}
interface A {
address: string;
}

const user: A = {
name: 'aaa',
address: 'bbb'
};

写在最后

  1. 采用声明合并的做法解决该问题,好处是保证了自定义配置的类型安全。如果不考虑这里的类型安全,当然也有别的做法,比如我们使用as unknown as AxiosRequestConfig,skip掉类型检测,或者自行二次包装下Axios请求方法,对比而言,声明合并才是最简单正确的做法。
  2. TS只是类型加持,对JS功能本身并无effect

--

--

--

Develop with pleasure!

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alan He

Alan He

Develop with pleasure!

More from Medium

Intro to GET Requests

Troubleshooting the CORS error

How does CORS work?

How to solve npm ERR! cb() never called