Answer the question
In order to leave comments, you need to log in
What's wrong with function overload signature?
Linter is unhappy and issues this:
This overload signature is not compatible with its implementation signature .ts(2394)
The implementation signature is declared here.
function getProductCollection(func: (error: Error, data: null, info: string) => any): void;
function getProductCollection(func: (error: null, data: ProductUnit[], info: string) => any): void;
function getProductCollection(func: (error: null | Error, data: ProductUnit[] | null, info: string) => any): void { }
Answer the question
In order to leave comments, you need to log in
The overload implementation must contain in each argument a union from all possible combinations, that is, in your case like this:
function getProductCollection(func:
| ((error: Error, data: null, info: string) => any)
| ((error: null, data: ProductUnit[], info: string) => any)
): void {}
((error: Error, data: null, info: string) => any) | ((error: null, data: ProductUnit[], info: string) => any)
It's not at all the same as(error: null | Error, data: ProductUnit[] | null, info: string) => any
Well, by the way, overload in this case is not particularly needed, and even harmful . function getProductCollection(func: {
(error: Error, data: null, info: string): any;
(error: null, data: ProductUnit[], info: string): any;
}): void {}
This will protect the internals of getProductCollection from being called incorrectly, but on the outside, you will get a generic type in the callback:(error: null | Error, data: ProductUnit[] | null, info: string) => any
This will force the calling code to either do unnecessary checks or cast types, so if you don't need strong guarantees within a single function, then you can get away with a simple option:function getProductCollection(func: (error: null | Error, data: ProductUnit[] | null, info: string) => any): void { }
What did I miss?
(error: Error, data: null, info: string) => any
null
instead of Error
. function getProductCollection(func: (error: null | Error, data: ProductUnit[] | null, info: string) => any): void;
function getProductCollection(func: (error: Error, data: null, info: string) => any): void { }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question