【问题标题】:Typescript, index.d.ts and webpack: Module not found errorTypescript、index.d.ts 和 webpack:找不到模块错误
【发布时间】:2016-11-10 03:04:43
【问题描述】:

我使用 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,但随后我收到了不同的错误消息。我错过了什么吗?

【问题讨论】:

    标签: typescript


    【解决方案1】:

    想出了一个解决方案,将 index.ts 用于所有导出,而不是 index.d.ts。

    【讨论】:

      【解决方案2】:

      对我来说,我最终将其从 './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评论:

      我可以确认这个问题,@Draccoz @filipesilva。

      将文件名从 .d.ts 更改为 .ts 时不会发生这种情况。 当您从 import { Something } from 'yourlib' 更改您的导入时,它也不会发生;从'yourlib.d'导入{Something};

      我刚刚将一个工作项目从 webpack-starter 种子(TypeScript/Angular)移动到 CLI 并收到此错误,因此我认为它与生成的 CLI 项目有关。

      我正在使用@angular/cli: 1.0.0-rc.2


      我认为还值得一提的是 tsx 文件可以正常工作。我必须将 types 更改为 types.d 仅用于 ts 文件。

      【讨论】:

        猜你喜欢
        • 2021-09-13
        • 2017-09-08
        • 2017-10-13
        • 2016-07-23
        • 2015-06-12
        • 1970-01-01
        • 1970-01-01
        • 2016-10-17
        • 1970-01-01
        相关资源
        最近更新 更多