我是打字稿初学者,在使用promisify转换fs.stas时遇到了一些打字稿警告。
const stat: (
pathname: string
) => Promise<fs.Stats | NodeJS.ErrnoException> = util.promisify(fs.stat);
Cannot invoke an expression whose type lacks a call signature. Type 'Stats' has no compatible call signatures.ts(2349)
答案 0 :(得分:0)
您的tsconfig.json
和package.json
是什么样的?
以下对我有用:
import fs from "fs";
import util from "util";
const stat: (pathname: string) => Promise<fs.Stats> = util.promisify(fs.stat);
tsconfig.json
:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"esModuleInterop": true
}
}
package.json
:
{
"dependencies": {
"@types/node": "^11.11.8",
"typescript": "^3.3.4000"
}
}
此外,您不应该在承诺的类型中指定错误类型stat
。因此,您应该只做Promise<fs.Stats | NodeJS.ErrnoException>
Promise<fs.Stats>