如果不提供文件扩展名,则无法导入TypeScript模块

Ste*_*tri 5 javascript node-modules typescript ecmascript-6 es6-module-loader

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

我要做

import {sealed} from "./decorators/decorators.ts";
Run Code Online (Sandbox Code Playgroud)

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

import {sealed} from "./decorators/decorators";
Run Code Online (Sandbox Code Playgroud)

这会导致错误,表明它只查找以.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"
]
Run Code Online (Sandbox Code Playgroud)

}

thi*_*ple 8

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

resolve: {
  extensions: ['', '.js', '.jsx', '.ts', '.tsx']
},
Run Code Online (Sandbox Code Playgroud)


Mar*_*ssy 5

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

resolve: {
  extensions: ['.js', '.jsx', '.ts', '.tsx']
},
Run Code Online (Sandbox Code Playgroud)