繁体   English   中英

不提供文件扩展名就无法导入TypeScript模块

[英]Can't import TypeScript modules without providing the file extension

我对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"
]

}

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM