VDone Demo VDone Demo
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)

Nikolay Tuzov

Backend Developer
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)
  • 初识 TypeScript

  • TypeScript 常用语法

  • ts-axios 项目初始化

  • ts-axios 基础功能实现

  • ts-axios 异常情况处理

  • ts-axios 接口扩展

    • Extending Interfaces
    • axios Function Overloading
      • Requirements Analysis
      • Overloading Implementation
      • Writing the Demo
    • Generic Support for Response Data
  • ts-axios 拦截器实现

  • ts-axios 配置化实现

  • ts-axios 取消功能实现

  • ts-axios 更多功能实现

  • ts-axios 单元测试

  • ts-axios 部署与发布

  • 《TypeScript 从零实现 axios》
  • ts-axios 接口扩展
HuangYi
2020-01-05
Contents

axios Function Overloading

# axios Function Overloading

# Requirements Analysis

Currently, our axios function only supports passing 1 argument, as follows:

axios({
  url: '/extend/post',
  method: 'post',
  data: {
    msg: 'hi'
  }
})
1
2
3
4
5
6
7

We want the function to also support passing 2 arguments, as follows:

axios('/extend/post', {
  method: 'post',
  data: {
    msg: 'hello'
  }
})
1
2
3
4
5
6

The first argument is the url, and the second argument is config. This function is somewhat similar to the argument types supported by the axios.get method, except that if we want to specify the HTTP method type, we still need to pass method in config.

This is where the function overloading concept we learned earlier comes in. Let's implement it.

# Overloading Implementation

First, we need to modify the AxiosInstance type definition.

types/index.ts:

export interface AxiosInstance extends Axios {
  (config: AxiosRequestConfig): AxiosPromise

  (url: string, config?: AxiosRequestConfig): AxiosPromise
}
1
2
3
4
5

We added a new function definition that supports 2 arguments, where url is a required parameter and config is an optional parameter.

Since the axios function actually points to the request function, let's modify the request function's implementation.

core/Axios.ts:

  request(url: any, config?: any): AxiosPromise {
    if (typeof url === 'string') {
      if (!config) {
        config = {}
      }
      config.url = url
    } else {
      config = url
    }
    return dispatchRequest(config)
  }
1
2
3
4
5
6
7
8
9
10
11

We changed the request function's parameters to 2, with both url and config being any type, and config remaining optional.

In the function body, we check whether url is a string type. If it is a string type, we proceed to check config, since it might not be passed. If it's empty, we construct an empty object and then add url to config.url. If url is not a string type, it means we passed a single argument, and url is actually config, so we assign url to config.

Note that although we modified the request implementation to support 2 types of parameters, the externally provided request interface remains unchanged. Think of this as merely an internal implementation change that doesn't need to match the external interface -- it only needs to remain compatible with the interface.

# Writing the Demo

examples/extend/app.ts:

axios({
  url: '/extend/post',
  method: 'post',
  data: {
    msg: 'hi'
  }
})

axios('/extend/post', {
  method: 'post',
  data: {
    msg: 'hello'
  }
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14

We used both ways of calling axios. Open the page and run the demo. Through the network panel, we can see that both request methods are working correctly.

At this point, we have implemented function overloading for axios. The official axios supports a capability where we can define the type of the return data and specify that type when making a request, so that we can access the data type in the response data. In the next section, we will implement this feature.

Edit (opens new window)
#TypeScript
Last Updated: 2026/03/21, 12:14:36
Extending Interfaces
Generic Support for Response Data

← Extending Interfaces Generic Support for Response Data→

Recent Updates
01
How I Discovered Disposable Email — A True Story
06-12
02
Animations in Grid Layout
09-15
03
Renaming a Git Branch
08-11
More Articles >
Theme by VDone | Copyright © 2026-2026 Nikolay Tuzov | MIT License | Telegram
  • Auto
  • Light Mode
  • Dark Mode
  • Reading Mode