gpt4 book ai didi

Typescript 2 找不到自定义定义

转载 作者:搜寻专家 更新时间:2023-10-30 21:12:40 24 4
gpt4 key购买 nike

我正在尝试在我的 typescript 项目中使用 NPM 的一些外部 js 库,不幸的是这些库没有提供 @types 定义,所以我开始自己编写。当我将它们嵌套在 ./node_modules/@types/<library_name>/*.d.ts 中时,我编写的定义起作用了但是一旦我将它们移动到例如./@types/<library_name>/*.d.ts它们没有被编译器拾取,我收到以下错误:

ERROR in ./utils/eventEmitter.ts
(1,25): error TS2307: Cannot find module 'events'.

我正在使用:TypeScript 版本:来自 npm 的 2.0.3

我有以下项目结构设置

@types/
-- events
---- index.d.ts
node_modules/
-- @types
-----react, react-dom etc
utils/
-- eventEmitter.js
tsconfig.json

在我的 tsconfig.json 中我有:

{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"target": "es5",
"jsx": "react",
"allowJs": true,
"sourceMap": true,
"typeRoots" : [
"./@types"
],
"types" : [
"events",
"ajax",
"path",
"keymirror"
]
},
"exclude" : [
"node_modules",
"@types"
]
}

eventEmitter.js 我有以下内容:

import * as events from "events";

只是为了澄清具体的事件模块 JS 位于 ./node_modules/events/index.js

最佳答案

我的问题所描述的问题已被确认为 typescript 中的错误(我使用的是 2.0.3)。

解决方案是在tsconfig.json中加入如下配置:

{
"compilerOptions": {
...
"paths": {
"*": ["*", "./@types/*"]
}
...
}
}

根据:github issue

关于Typescript 2 找不到自定义定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39914569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com