在Node.js和TypeScript中找不到错误堆栈跟踪的名称“CallSite”

pb3s4cty  于 2022-12-30  发布在  TypeScript
关注(0)|答案(1)|浏览(129)

我有这个代码:

Error.prepareStackTrace = function (
  error: Error,
  stack: Array<CallSite>,
) {
  return self.parse(fiber, error, stack)
}

我正在尝试导入CallSite,但在@types/node中找不到它。

tsconfig.json

{
  "compilerOptions": {
    "module": "ES6",
    "target": "ES6",
    "lib": ["es2020"],
    "outDir": "host",
    "rootDir": "make",
    "sourceMap": true,
    "moduleResolution": "node",
    "strictNullChecks": true,
    "strict": true,
    "esModuleInterop": true,
    "noUncheckedIndexedAccess": true,
    "baseUrl": "./make",
    "paths": {
      "~": ["."]
    },
    "types": ["node"],
    "typeRoots": ["node_modules/@types"],
    "noImplicitAny": true
  },
  "include": ["make"],
  "exclude": ["node_modules"]
}

package.json

{
  "type": "module",
  "scripts": {
    "build": "tsc && tsc-alias",
    "watch": "concurrently --kill-others \"tsc -w\" \"tsc-alias -w\"",
    "lint": "eslint --ext .ts ./make",
    "lint:fix": "npm run lint -- --fix",
    "test": "node --no-warnings --loader @esbuild-kit/esm-loader host/build"
  },
  "devDependencies": {
    ...
    "@types/node": "^18.11.17",
    "ts-node": "^10.9.1",
    "tsc-alias": "^1.8.2",
    "typescript": "^4.9.4"
  }
}

你知道为什么它找不到它,或者怎么找到它吗?

lsmepo6l

lsmepo6l1#

如果检查源代码,CallSite位于名称空间NodeJS中。
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/0cb820adf9389fbda935c850f58eb2c5e5973515/types/node/globals.d.ts#L125
所以你需要像这样修改你的声明。

Error.prepareStackTrace = function (
    error: Error,
    stack: Array<NodeJS.CallSite>,

相关问题