ducin
ducin

Reputation: 26467

proper way to override function with different param types in TypeScript

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.

Upvotes: 0

Views: 1765

Answers (1)

Ryan Cavanaugh
Ryan Cavanaugh

Reputation: 221044

It slices! it dices! It makes toast!

// Signatures
function formatAPI(): string[];
function formatAPI(name: string): string;
function formatAPI(name: string, callback: (arg: string) => void): void;
function formatAPI(map: { [name: string]: () => string }): void;
// Implementation
function formatAPI(nameOrMap?: string | { [name: string]: () => string }, callback?:(arg: string) => void): string | string[] | void {
  if (typeof nameOrMap === 'string') {
    if (callback) {
      return registry.register(nameOrMap, callback);
    } else {
      return registry.get(nameOrMap);
    }
  } else if (...) {
    // Keep adding checks here
  }
}

One thing to note is that the compiler doesn't use the signatures to infer behavior for type guards. This means you should structure your code slightly more conservatively -- here, I put both string cases inside the typeof check.

Upvotes: 4

Related Questions