AWS Javascript SDK v3 - 由于错误 TS2304,Typescript 无法编译:找不到名称“ReadableStream”

Ker*_*uba 9 amazon-web-services typescript aws-sdk-js

我正在尝试构建使用 AWS Javascript SDK v3 的项目。这里我的tsconfig.json

{
  "compilerOptions": {
    "target":"ES2020",
    "module": "commonjs",
    "lib": ["es2020"],
    "outDir": "dist",
    "resolveJsonModule": true,
  },
  "exclude": [
    "coverage",
    "node_modules",
    "dist",
    "tests"
  ]
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的构建错误示例(为了简洁起见,我已经删除了一些输出):

node_modules/@aws-sdk/client-s3/types/models/models_1.d.ts:727:23 - error TS2304: Cannot find name 'ReadableStream'.

node_modules/@aws-sdk/client-s3/types/models/models_1.d.ts:727:40 - error TS2304: Cannot find name 'Blob'.

node_modules/@aws-sdk/util-dynamodb/types/models.d.ts:19:86 - error TS2304: Cannot find name 'File'.
Run Code Online (Sandbox Code Playgroud)

我不明白为什么这会给我这样的问题,即使我已经安装了@types/node支持节点输入的模块

Ker*_*uba 18

原来,为了让打字稿找到Blob,File等名字,我不得不添加的dom条目的lib在我tsconfig.json。这是我的最终 tsconfig,它允许我正确构建项目

{
  "compilerOptions": {
    "target":"ES2020",
    "module": "commonjs",
    "lib": ["es2020", "dom"],
    "outDir": "dist",
    "resolveJsonModule": true,
  },
  "exclude": [
    "coverage",
    "node_modules",
    "dist",
    "tests"
  ]
}
Run Code Online (Sandbox Code Playgroud)

  • 这为我解决了这个问题。但是,我的应用程序在 Nodejs 上运行。将“dom”添加到该项目(可能会检查浏览器兼容性)是否会出现任何问题?奇怪的是“AWS”无法针对普通 Nodejs 进行编译? (10认同)
  • 如果我们在节点应用程序中添加“dom”,它会引发更多类型冲突和错误。有谁知道其他解决方案? (2认同)

Wal*_*llo 18

不将domlib 包含在tsconfig.json

// src/@types/dom.ts (or any file included in the tsc compilation)

export {}

declare global {
  type ReadableStream = unknown
  type Blob = unknown
}

Run Code Online (Sandbox Code Playgroud)