網站搜尋

javascript - 找不到名稱 'ReadableStream' (simple-git, node)


我已經查看了所有的網路資源,但無法為我的新專案找到解決方案。我正在使用 simple-git 套件,而其型別定義中包含了幾個對 ReadableStream 的引用。Typescript 在轉譯時會拋出錯誤("找不到名稱 'ReadableStream'")。我已經進一步縮小了問題,如下所示。

package.json

{
  "name": "my-project",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "build": "./node_modules/.bin/tsc",
    "watch": "./node_modules/.bin/tsc --watch"
  },
  "devDependencies": {
    "@types/node": "^10.7.1",
    "@types/yargs": "^11.1.1",
    "typescript": "^3.0.1"
  },
  "dependencies": {
    "fs": "0.0.1-security",
    "simple-git": "^1.96.0",
    "yargs": "^12.0.1"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "lib": [
      "esnext"
    ],
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": false,
  }
}

./src/index.ts

const s: ReadableStream = null;

...導致此錯誤:

> npm run build

src/index.ts:6:10 - 錯誤 TS2304:找不到名稱 'ReadableStream'。

6 const s: ReadableStream=null;

答案#1

estus 是對的。回到原本的重點,看起來這是 simple-git 類型定義中的一個錯誤。你可以提交一個錯誤報告。與此同時,在全域宣告檔案中寫 import ReadableStream=NodeJS.ReadableStream; 應該可以解決這個問題。

答案#2

Node 全域型別是在 NodeJS 命名空間下宣告的。應該是:

const s: NodeJS.ReadableStream = null;