我可以在cb
的Typescript中进行重载吗?它表示如果Error
为空,则必须定义Id
,或者如果定义了Error
,则必须Id
为空?
function passId(cb: (err: Error, id: null) => void): any
function passId(cb: (err: null, id: number) => void): any
function passId(cb: (err: Error | null, id: number | null) => void){
// implementation ...
}
// usage: if error defined and the fn returns
// then it means because of the type definition number must be defined.
const foo = () => {
if(err){
return
}
// id must be defined by here..
let a: number = id
}