如何在纯JavaScript环境中使用UMD打字稿模块

use*_*997 5 typescript

我希望打字稿将我的* .ts文件转换为umd * .js文件。问题是我无法在纯JavaScript环境中使用模块,因为这些模块赢得了t be set into thewindow对象。

tsconfig.json

{
  "compilerOptions": {
    "module": "umd",
    "removeComments": true,
    "preserveConstEnums": true,
    "sourceMap": true
  },
  "include": [
    "src/**/*.ts"
  ]
}
Run Code Online (Sandbox Code Playgroud)

src / Car.js

export default class Car {
    drive() {
        console.log('Driving');
    }
}
Run Code Online (Sandbox Code Playgroud)

生成的js文件:

(function (dependencies, factory) {
    if (typeof module === 'object' && typeof module.exports === 'object') {
        var v = factory(require, exports); if (v !== undefined) module.exports = v;
    }
    else if (typeof define === 'function' && define.amd) {
        define(dependencies, factory);
    }
})(["require", "exports"], function (require, exports) {
    "use strict";
    var Car = (function () {
        function Car() {
        }
        Car.prototype.drive = function () {
            console.log('Driving');
        };
        return Car;
    }());
    exports.__esModule = true;
    exports["default"] = Car;
});
//# sourceMappingURL=Car.js.map
Run Code Online (Sandbox Code Playgroud)

在我的index.html中,我希望能够像这样调用drive:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Title</title>

    <script src="src/Car.js"></script>
</head>
<body>

    <script>
        // On document ready
        var car = new Car();
        car.drive();
    </script>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

打字稿可以吗?你能举个例子吗?

编辑:在这篇文章中,我可以阅读到UMD TypeScript不能涵盖标准的js情况:https : //github.com/Microsoft/TypeScript/issues/3322 这仍然是正确的吗?