我使用 webpack 创建了 Typescript 项目,遵循 this tutorial . 因为我有很多模块,所以我将 index.d.ts 添加到我导出所有模块的 src/components 文件夹中:
export {Module1} from "./Module1";
export {Module2} from "./Module2";
export {Module3} from "./Module3";
我将 index.d.ts 添加到 tsconfig.json 文件中:
"files": [
"./src/components/index.d.ts"
]
然后,在 src 文件夹中的 index.tsx 中,我导入:
import * as MyModule from "./components/";
代码提示工作正常,所以我想所有路径都可以。 但是,当我运行 webpack 时,出现此错误:
未找到模块:错误:无法解析目录“./components”
据我了解,webpack 找不到这个 index.d.ts 文件。我尝试将 d.ts 添加到 webpack.config.js 扩展中,比如 described here ,但后来我收到了不同的错误消息。我错过了什么吗?
最佳答案
对我来说,我最终将它从 './types'
更改为 './types.d'
。
所以类型导入看起来像,
import {
TSomeType,
ISomeInterface,
ESomeEnum
} from 'src/types/components/SomeComponent/types.d'
而不是,
import {
TSomeType,
ISomeInterface,
ESomeEnum
} from 'src/types/components/SomeComponent/types'
取自this评论:
I can confirm this issue, @Draccoz @filipesilva .
It does not happen when you change the filename from .d.ts to .ts. It also does not happen when you change your import from import { Something } from 'yourlib'; to import { Something } from 'yourlib.d';
I just moved a working project from a webpack-starter seed (TypeScript/Angular) to CLI and got this error, so I assume it has something to do with the generated CLI project.
I'm using @angular/cli: 1.0.0-rc.2
我认为还值得注意的是 tsx 文件工作得很好。我必须将 types
更改为 types.d
仅适用于 ts 文件。
关于Typescript、index.d.ts 和 webpack : Module not found error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38251505/