If possible, remove the overloads with any
. I'm not sure if that's applicable in this case; it depends on how the API works.
res1
will always be OK because you're not required to consume parameters in TypeScript (imagine how annoying it would be if you had to have a three-parameter callback to Array#forEach).
For res2
, you can sometimes improve the situation by adding "trap" overloads:
interface IResourceClass<T> {
get(): T;
get(success: IResourceSuccessCallbackFunction<T>, error?: IResourceErrorCallbackFunction): T;
get(otherFunction1: (...a: any[]) => void, otherFunction2: (...a: any[]) => void): { YOU_HAVE_FAILED: {}; };
get(dataOrParams: any): T;
get(dataOrParams: any, success: IResourceSuccessCallbackFunction<T>): T;
get(params: any, data: any, success?: IResourceSuccessCallbackFunction<T>, error?: IResourceErrorCallbackFunction): T;
}
var resourceService: IResourceClass<number> = null;
var res1 = resourceService.get({ id: 1000 }, function() {
console.log("fail");
});
// res2: { YOU_HAVE_FAILED: {} }
var res2 = resourceService.get(function(p: Phone) {
console.log(p);
}, function() {
console.log("fail");
});
This won't be a compile error immediately, but any practical use of res2
will be an error.