有没有一种方法可以将Typescript编译到es6中,但可以强制它使用requirejs?

时间:2020-06-17 12:32:35

标签: javascript node.js typescript module

我正在编写一个NodeJS打字稿项目。假设我有以下文件:

class MyArray extends Array {
   myCustomMethod() { /* do something */ }
}

但是,当我这样做

let myarray = new MyArray();

myarray.myCustomMethod();

我知道

TypeError: myarray.myCustomMethod is not a function

因此,我尝试将打字稿module更改为es6(以便保留class extends语法),但是随后使用了importexport关键字,而NodeJS本身不支持。

有没有一种方法可以强制打字稿编译器使用requirejs但还要使用es6?

是我的坏事,我想念什么吗?

1 个答案:

答案 0 :(得分:0)

TypeScript的--module选项(也为-m)使您可以指定输出的模块系统。 "CommonJS"是使用require的变体。 project configuration options documentation中的详细信息。

例如,在tsconfig.json中,"module": "CommonJS""target": "ES2015"(或"ES2020""ESNext"等组合)应使用{{1 }}和require语法。

相关问题