简体   繁体   English

打字稿es6导入语法输出es5 commonjs模块问题

[英]typescript es6 import syntax output es5 commonjs module issue

So the issue I have is that I am trying to use knockout-es5 and output to common js format, but I am writing everything using the es6 syntax. 因此,我遇到的问题是我正在尝试使用敲除-es5并将其输出为通用js格式,但是我正在使用es6语法编写所有内容。

Now the problem I have is that I do the following: 现在我遇到的问题是我执行以下操作:

import ko from "knockout-es5";

with the hope that the output when ran through tsc would be: 希望通过tsc运行时的输出为:

var ko = require("knockout-es5");

However first of all it wont compile because of the knockout-es5 descriptor file doesnt have a module export, so I went in and added the following to the end of it: 但是首先,由于knockout-es5描述符文件没有模块导出,因此它无法编译,因此我进入了程序,并在其末尾添加了以下内容:

declare var ko: KnockoutStatic; export module "knockout-es5" { export = ko; } declare var ko: KnockoutStatic; export module "knockout-es5" { export = ko; } Which then does seem to work, but if you look at the output where it gets used it makes it do ko.default.yourMethod rather than ko.yourMethod which then doesn't work. declare var ko: KnockoutStatic; export module "knockout-es5" { export = ko; }这似乎确实起作用,但是如果您查看使用它的输出,它将使它执行ko.default.yourMethod而不是ko.yourMethod ,这将不起作用。

So I am a bit baffled, as I love the ES6 syntax and my code is all written using ES6 imports and exports, but as I am dependent upon non ES6 exported modules I cannot seem to find a workable middle ground, I either remove the knockout default export and no require is included in the output file, or I add the default export and it adds the default property. 所以我有点困惑,因为我喜欢ES6语法,并且我的代码都是使用ES6导入和导出编写的,但是由于我依赖于非ES6导出的模块,因此我似乎找不到可行的中间立场,因此我要么删除了敲除项默认导出并且没有要求包含在输出文件中,或者我添加了默认导出,并添加了default属性。

So is there a way to have my cake and eat it here? 那么,有什么办法可以在这里吃蛋糕吗?

You probably want 你可能想要

import * as ko from "knockout-es5";

Your original import is the same as import {default as ko} from ... . 您的原始导入与import {default as ko} from ...相同。

Refer to http://www.2ality.com/2014/09/es6-modules-final.html for a comprehensive explanation of the syntax. 有关语法的全面说明,请参见http://www.2ality.com/2014/09/es6-modules-final.html

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

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