打字稿es6导入语法输出es5 commonjs模块问题 [英] typescript es6 import syntax output es5 commonjs module issue

查看:97
本文介绍了打字稿es6导入语法输出es5 commonjs模块问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我遇到的问题是我正在尝试使用敲除-es5并输出为通用的js格式,但是我正在使用es6语法编写所有内容.

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.

现在我遇到的问题是我要执行以下操作:

Now the problem I have is that I do the following:

import ko from "knockout-es5";

希望通过tsc运行时的输出为:

with the hope that the output when ran through tsc would be:

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

但是首先,由于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:

declare var ko: KnockoutStatic; export module "knockout-es5" { export = ko; } 看起来确实可行,但是如果您查看输出的使用位置,它将使其执行ko.default.yourMethod而不是ko.yourMethod,这将不起作用.

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.

所以我有点困惑,因为我喜欢ES6语法,并且我的代码都是使用ES6导入和导出编写的,但是由于我依赖于非ES6导出的模块,因此我似乎找不到可行的中间立场,我要么删除基因剔除的默认导出,并且输出文件中不包含任何要求,或者添加默认导出,并添加default属性.

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.

那么有办法在这里吃我的蛋糕吗?

So is there a way to have my cake and eat it here?

推荐答案

您可能想要

import * as ko from "knockout-es5";

您的原始导入与import {default as ko} from ...相同.

请参阅 http://www.2ality.com/2014/09/es6-modules-final.html 对该语法进行了全面的解释.

Refer to http://www.2ality.com/2014/09/es6-modules-final.html for a comprehensive explanation of the syntax.

这篇关于打字稿es6导入语法输出es5 commonjs模块问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆