Java 模块 添加_如何在子目录中添加模块的类型定义?

我试图使用nock模拟使用axios发出的请求 . 这种集成在历史上存在一些问题,但是in this issue描述的解决方法有效 . 当我将Typescript引入混合时,我的问题出现了 . 虽然 axios 附带了主库的类型定义文件,但 nock 和 axios 集成需要从子目录导入,如下所示:

import HttpAdapter from 'axios/lib/adapters/http';

Typescript编译器抱怨此行有以下错误:

TS7016: Could not find a declaration file for module 'axios/lib/adapters/http'. 'C:/Users///node_modules/axios/lib/adapters/http.js' implicitly has an 'any' type.

Try `npm install @types/axios` if it exists or add a new declaration (.d.ts) file containing `declare module 'axios';`

我该如何解决这个错误?我尝试了 axios 的各种自定义类型定义组合:

// /src/types/axios.d.ts

declare module 'axios/lib/adapters/http` {

import { Adapter } from 'axios';

const HttpAdapter: Adapter;

export default HttpAdapter;

}

我对Typescript不太熟悉,所以我的理论是我的自定义定义文件位置错误,使用了错误的模块名称,或者没有导出正确的东西 . 任何帮助将不胜感激 .

附加上下文

Typescript Version: 2.9.2

tsconfig.json 的(相关)内容:

{

"compilerOptions": {

"allowSyntheticDefaultImports": true,

"module": "esnext",

"target": "es5",

"lib": ["es2015", "es2017", "dom"],

"moduleResolution": "node",

"rootDir": "src",

"noImplicitAny": true,

"typeRoots": ["./src/types", "./node_modules/@types"]

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值