TS2345:X型商榷是不能分配给Y型的参数与打字稿
问题描述:
奇怪的错误:TS2345:X型商榷是不能分配给Y型的参数与打字稿
作为图像表示的,错误是:
TS2345: Argument of type 'ErrnoException' is not assignable to parameter of type '(err: ErrnoException) => void'. Type 'ErrnoException' provides no match for the signature '(err: ErrnoException): void'.
这里是导致错误的代码:
export const bump = function(cb: ErrnoException){
const {pkg, pkgPath} = syncSetup();
fs.writeFile(pkgPath, JSON.stringify(pkg, null, 2), cb);
};
有人知道这里发生了什么?
答
您发送的值为ErrnoException,而您调用的函数需要一个函数,该函数接受一个* ErrnoException **类型的参数并返回void。
您发送:
let x = new ErrnoException;
当你调用该函数预计
let cb = function(e: ErrnoException) {};
你可以改变你的函数接收这样正确的参数。
export const bump = function(cb: (err: ErrnoException) => void){
const {pkg, pkgPath} = syncSetup();
fs.writeFile(pkgPath, JSON.stringify(pkg, null, 2), cb);
};