2017-08-31 57 views
0

奇怪的錯誤:TS2345:X型商榷是不能分配給Y型的參數與打字稿

enter image description here

作爲圖像表示的,錯誤是:

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); 
}; 

有人知道這裏發生了什麼?

回答

2

您發送的值爲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); 
}; 
相關問題