【问题标题】:Can't import TypeScript modules without providing the file extension无法在不提供文件扩展名的情况下导入 TypeScript 模块
【发布时间】:2016-07-30 08:50:06
【问题描述】:

我是 TypeScript 的新手,我希望我应该能够导入我的 TS 文件,而无需指定它们是 TS 文件。

我必须这样做

import {sealed} from "./decorators/decorators.ts";

而不是我想要的正确方法是

import {sealed} from "./decorators/decorators";

这会导致错误,表明它只是在寻找以 .js 或 .jsx 结尾的文件

我的 tsconfig.json 看起来像这样

{
"compileOnSave": true,
"compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "node",
    "jsx": "react",
    "allowJs": true,
    "target": "es6",
    "removeComments": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
},
"exclude": [
    "node_modules",
    "typings/browser",
    "typings/browser.d.ts"
]

}

【问题讨论】:

  • 我看到您将模块编译成 commonjs 模块。你用什么来加载它们?网络包?还是这是一个节点应用程序?
  • 我正在使用 Webpack

标签: javascript typescript ecmascript-6 node-modules es6-module-loader


【解决方案1】:

如果您使用的是 webpack,请尝试将其添加到您的 webpack.config.js 文件中:

resolve: {
  extensions: ['', '.js', '.jsx', '.ts', '.tsx']
},

【讨论】:

  • 天啊,就是这样。谢谢!
【解决方案2】:

thitemple 的回答对我有用,但我不得不从数组中删除 '',因为它阻止了 webpack 的启动。

resolve: {
  extensions: ['.js', '.jsx', '.ts', '.tsx']
},

【讨论】:

    猜你喜欢
    • 2021-04-29
    • 2017-10-13
    • 1970-01-01
    • 2020-04-03
    • 2011-07-20
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 2016-01-31
    相关资源
    最近更新 更多