I'm trying to create a union type for arrow functions.
type ItemType = {
createTime: number;
totalTime: number;
text: string;
};
type ObjType = {
callback: ((item: string) => void) | ((item: ItemType) => void);
}
let obj: ObjType = {
callback: (item: string) => {
console.log(item)
}
}
So far it works well.
And then I am going to call the function.
obj.callback('text');
It shows an error
Argument of type 'string' is not assignable to parameter of type 'string & ItemType'.
Type 'string' is not assignable to type 'ItemType'.
The type of callback became (item: string & ItemType) => void.
What am I doing wrong?
callbackbecomes(item: string & ItemType) => void. I have no idea what is happening.Argument of type XXX is not assignable to parameter of type 'never'. If you see this error in the question it is 90% that OP has union of functions