"Can't invoke class without new" 和 "Can't redeclare blocked-scoped variable" 同时发生

"Can't invoke class without new" and "Can't redeclare blocked-scoped variable" happening at the same time

我在尝试使用 RESTDataSource 时开始遇到错误:

"Class constructor RESTDataSource cannot be invoked without 'new'"

所以我尝试了 this 解决方案并添加了 "target": "es2016"。我不再收到错误消息。

但是现在我遇到了打字稿编译错误 error TS2451: Cannot redeclare block-scoped variable 'Query'.

显然这是因为 typescript 无法将我的文件识别为模块。

所以我尝试了 解决方案并将 export {} 添加到我的文件中,以便它们被识别为模块。但是因为我的目标是 es2016 我得到 Unexpected token 'export'

有什么办法可以同时解决这两个问题吗?

错误遍及我的代码,因此我将包含整个存储库:https://github.com/grochadc/filex-graphql-server

tsconfig.json

{
  "compilerOptions": {
    "target": "es2016",
    "moduleResolution": "node",
    "outDir": "dist/",
    "allowSyntheticDefaultImports": true
  },
  "include": ["src/"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}

typescript docs

的相关栏目拉出来

If you had the following Node/CommonJS code:

var foo = require("foo");

foo.doStuff();

then you would write the following TypeScript code:

import foo = require("foo");

foo.doStuff();

exporting

You might have previously written that like so:

function foo() {
   // ... 
} 
module.exports = foo;

In TypeScript, you can model this with the export = construct.

function foo() {
  // ...
}
export = foo;

然后,如果您在 tsconfig 中设置 "module": "CommonJS",它会将这些转换回您现在拥有的内容,这是一个遗留限制,如果您没有导入或导出结构,它会假定它不是模块,因此您只需使用 importexport

将文件标记为模块