Typescript导入函数会出现编译错误

时间:2017-10-19 09:51:53

标签: typescript visual-studio-2017

在我的Typescript中,我使用的是导入函数,如下所述:

https://github.com/Microsoft/TypeScript/issues/12933

所以我的代码就像:

 import("../myScriptToBeImported").then((module) => {
                this.dosomethingWithModule(module);
            }).catch(this.doSomethingWithError.bind(this));

我在使用Webpack的Visual Studio中运行此作为MVC项目的一部分。 Webpack编译Typescript没有任何错误,项目运行正常,但对于上面的行,以下错误总是在Visual Studio中显示:

  • TS1128(TS)预期的声明或声明。
  • TS2391(TS)功能 实施缺失或不紧跟在 宣言。
  • TS7010(TS)'import',缺少返回类型 注释,隐含地具有“任何”返回类型。

我正在使用Typescript版本2.4.2。我怎样才能摆脱这些错误?

有关信息,我的tsconfig文件如下所示:

{
  "compilerOptions": {
    "module": "esnext",
    "moduleResolution": "node",
    "noEmitOnError": true,
    "strict": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [ "es6", "dom" ],
    "baseUrl": ".",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  },
  "exclude": [
    "node_modules",
    "wwwroot"
  ],
  "compileOnSave": false,
  "buildOnSave": false
}

1 个答案:

答案 0 :(得分:1)

函数式import还不是JS模块规范的一部分。您需要在module中将"esnext"设置为tsconfig.json才能使用它。

如果您已经这样做了,问题可能在于Visual Studio - 确保您的项目配置为使用正确版本的TypeScript编译器。