Typescript axios没有导出的成员';取消令牌';
我正在将JavaScript代码库迁移到TypeScript。 在我的许多源文件中:Typescript axios没有导出的成员';取消令牌';,typescript,axios,Typescript,Axios,我正在将JavaScript代码库迁移到TypeScript。 在我的许多源文件中: import {CancelToken} from 'axios'; 。。。取消的效果非常好。迁移到TypeScript后,我得到: TS2305: Module '"./some/path/node_modules/axios/index.js"' has no exported member 'CancelToken'. 经调查,我发现文件node\u modules/axios/l
import {CancelToken} from 'axios';
。。。取消的效果非常好。迁移到TypeScript后,我得到:
TS2305: Module '"./some/path/node_modules/axios/index.js"' has no exported member 'CancelToken'.
经调查,我发现文件node\u modules/axios/lib/axios.js
(从axios/index.js
导入)确实包含一个命名成员CancelToken
:
axios.CancelToken = require('./cancel/CancelToken');
...
module.exports = axios;
(既然代码的Javascript版本起作用了,那又有什么不同呢)。
但奇怪的是,一个接口类型使用了完全相同的名称(可在node\u modules/axios/lib/index.t.ds
中找到):
。。。这给了我一个不同的信息:
TS2693: 'CancelToken' only refers to a type, but is being used as a value here
在我的代码中,我有:
const source = CancelToken.source();
。。。正是在上面这一行,TypeScript抱怨“[the]type[…]正被用作一个值”。我最初做的是:
import CancelToken from './some/path/node_modules/axios/lib/cancel/CancelToken.js';
。。。这似乎是一个黑客,我不知道为什么它是必要的。几天后,我发现了一个更好的方法:
我在tsconfig.json
文件的“compilerOptions”
结构中设置了“moduleResolution”:“node”
,然后使用:
const {CancelToken} = require('axios');
试一试
import CancelToken from './some/path/node_modules/axios/lib/cancel/CancelToken.js';
const {CancelToken} = require('axios');
import axios from 'axios'
const cancelToken = axios.CancelToken