简体   繁体   English

有没有办法将 typescript 编译成 es6 但强制它使用 requirejs?

[英]Is there a way to compile typescript into es6 but force it to use requirejs?

I am writing a NodeJS typescript project.我正在编写一个 NodeJS typescript 项目。 Let's say I have the following file:假设我有以下文件:

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

However, when I do但是,当我这样做时

let myarray = new MyArray();

myarray.myCustomMethod();

I see我懂了

TypeError: myarray.myCustomMethod is not a function

So, I tried to change the typescript module to es6 (so that it will keep the class extends syntax), but then it uses the import and export keyword, and NodeJS doesn't support that natively.因此,我尝试将 typescript module更改为 es6(这样它将保留class extends语法),但随后它使用了importexport关键字,而 NodeJS 本身并不支持它。

Is there a way to force the typescript compiler to use requirejs but also use es6?有没有办法强制 typescript 编译器使用 requirejs 但也使用 es6?

Or is it my bad and I am missing something?还是我的错,我错过了什么?

TypeScript's --module option (also -m ) lets you specify what module system it should output for. TypeScript 的--module选项(也是-m )允许您指定 output 应该用于哪个模块系统。 "CommonJS" is the variant that uses require . "CommonJS"是使用require的变体。 Details in the project configuration options documentation . 项目配置选项文档中的详细信息。

So for instance, in tsconfig.json , "module": "CommonJS" combined with "target": "ES2015" (or "ES2020" or "ESNext" , etc.) should output code using require and also class syntax. So for instance, in tsconfig.json , "module": "CommonJS" combined with "target": "ES2015" (or "ES2020" or "ESNext" , etc.) should output code using require and also class syntax.

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

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