Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
726 views
in Technique[技术] by (71.8m points)

ts引入axios后,axios方法使用不了

test.ts

import axios from 'axios'
console.log(axios.get('https://www.xxx.com'))

这样子的话会报错
image.png

TS2339: Property 'get' does not exist on type 'typeof Axios'.
这样可以修复,但是每次都需要类型断言

import axios from 'axios'
console.log((axios as any).get('https://www.xxx.com'))

求大佬求大佬


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

如下图所示, axios提供的.d.ts, 你import axios from 'axios'的时候, 匹配上的类型是AxiosStatic, 这个类型继承了AxiosInstance, 的缺是有get这个方法的. 然而你的报错提示的是 ‘typeof Axios’ 而不是 AxiosStatic, 是不是你自己覆盖了axios的类型定义
image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.6k users

...