我有这个简单的打字稿代码:
const y = Buffer.from("fds")
y.toString("hex")
但是我在“十六进制”上收到 tsc 类型错误,提示
Expected 0 arguments, but got 1.
toString 方法接受一个参数https://nodejs.org/api/buffer.html#buftostringencoding-start-end
所以看来我的类型对于这个标准库来说不知何故不是最新的?
╰─❯ node -v
v20.12.2
╰─❯ yarn tsc -v
Version 5.3.2
我正在跑步
yarn run tsc --noEmit --p ./src --incremental"
我的 tsconfig
{
"extends": "../tsconfig.base.json",
"include": ["./src/**/*", "./mmmEval"],
"exclude": ["node_modules", "_build", "dist"],
"compilerOptions": {
"target": "es2020",
"composite": true,
"module": "ESNext",
"baseUrl": "./src",
"paths": {
"#src/*": ["./*"]
},
"types": ["node"],
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"plugins": [{ "name": "grats-ts-plugin" }]
},
"grats": {
"graphqlSchema": "./src/gql/schema.generated.graphql",
"tsSchema": "./src/gql/schema.generated.ts",
"tsSchemaHeader": "// @ts-nocheck\n // This file was automatically generated and should not be edited.\n",
"nullableByDefault": false
},
}
tsconfig.base.json
{
"compilerOptions": {
"composite": true,
"target": "es2022",
"lib": ["es2022", "dom", "dom.iterable", "esnext.disposable", "ESNext.Disposable"],
"module": "CommonJS",
"preserveSymlinks": true,
"preserveValueImports": true,
"ignoreDeprecations": "5.0",
"noUnusedLocals": true,
"esModuleInterop": false,
"forceConsistentCasingInFileNames": true,
"noPropertyAccessFromIndexSignature": true,
"noUncheckedIndexedAccess": true,
"strict": true,
"skipLibCheck": true,
"importsNotUsedAsValues": "error",
"noFallthroughCasesInSwitch": true,
"isolatedModules": true
}
}
您需要向项目添加缺少的依赖项:
@types/node
节点的类型定义。
yarn add @types/node
这应该可以解决你的问题