Typescript、index.d.ts 和 webpack : Module not found error

标签 typescript

我使用 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/

相关文章:

javascript - Angular2 奇怪的硬编码对象与 http.get 对象问题

angular - 未捕获( promise ): TypeError: Cannot read property 'registerControl' of null

javascript - 如何将(Cordova)插件与 Ionic 2/TypeScript 一起使用?

typescript - ag-grid cellEditor 选择对象值

javascript - 使用 browserify 和 typescript 来包含 Node 模块

typescript - 如何让 CDK 只创建一个弹性 IP 地址以用于单个 EC2 实例 (AWS CDK)

visual-studio - 如何在 Visual Studio 中控制 TypeScript 格式

TypeScript 编译器 (tsc) 无法识别导入别名

javascript - 在另一个指令的点击事件上重新计算 ngx-datatable

typescript - 如何在 A-La-Carte 系统中使用 vuetify 加载程序时导入 vuetify/lib