TL;DR; my formatAPI
function below is very much not the TYpeScript way of overriding a function...
I've got a function that might be called either of following four ways in JavaScript:
jsf.format("format", function(){ return ... }); // string, function
jsf.format("format"); // string
jsf.format({
"format1": function(){ return ... },
"format2": function(){ return ... }
}); // map[string:function]
jsf.format(); // no params
Each signature executes different logic beneath. CUrrently I've got the following:
function formatAPI(name: string, callback?: Function): any {
if (callback) {
registry.register(name, callback);
} else if (typeof name === 'object') {
registry.registerMany(name);
} else if (name) {
return registry.get(name);
} else {
return registry.list();
}
}
but it's inconsistent - e.g. second parameter is string
. When I change it to string|Object
than it fails on my logic (registry.register(name, callback);
- name
here has to be a string and string|Object
is not assignable to string). I'm trying to find a clear and extensible solution (to be able to add more variations if I need to, in the future), I try to adapt another question but I fail.
I would appreciate help and/or advise on how should I declare all possible signatures for this function AND the function body.