首页 > 解决方案 > 打字稿不断在导入上方添加一个函数

问题描述

当 typescript 编译 Javascript 时,它会在顶部添加这个函数

var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    }
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
import { PortWidget } from "storm-react-diagrams";

所以我得到了错误

第 14 行:在模块体中导入;重新排序到顶部导入/第一

这是我的 tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "es6",
      "dom"
    ],
    "sourceMap": false,
    "allowJs": true,
    "jsx": "preserve",
    "noImplicitReturns": false,
    "noImplicitThis": false,
    "noImplicitAny": false,
    "strictNullChecks": false,
    "skipLibCheck": true,
    "strict": false,
    "suppressImplicitAnyIndexErrors": true,
    "noUnusedLocals": false,
    "watch": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    // "resolveJsonModule": true,
    //"noEmit": true,
    "isolatedModules": true
  },
  "exclude": [
    "node_modules",
    "build",
    "desktop",
    "public",
    "mobile",
    "tests",
    "src/player/*"
  ],
  "include": [
    "src/admin/react"
  ]
}

知道我如何解决这个问题吗?

我不明白为什么每次都会添加这个扩展功能。

标签: typescript

解决方案


在编译期间,TypeScript 可以生成帮助__extends程序,以确保相关功能在运行时有效。您可以将它们视为 polyfill。

默认情况下,助手将被内联在每个需要它们的文件中。在您的情况下,这是使用该extends子句的每个文件。

您可以选择退出此行为并自己提供所需的帮助程序——只是这次它们只会被导入一次。使用--importHelpers让 TypeScripttslib为您提取它们。

tsconfig.json


{
  "compilerOptions": {
    ...
    "importHelpers": true,
    ...
  }    
}

记得tslib在你的项目中安装:

npm install tslib

阅读手册中有关编译器选项的更多信息。


推荐阅读