Search code examples
typescriptconventions

Naming conventions for overload parameters in typescript


I need to have a function which takes in parameter as string or array of string.

delete(keyOrKeys : string | string[]){
   if(Array.isArray(keyOrKeys){
     // delete all keys in array
   } else {
     // delete single key.
   }
}

So, I need to follow good naming conventions. What name should I use in place of 'keyOrkeys'. Or is it just fine ?


Solution

  • If you create definition for this function with real overloads (not just one implementation), you can name argument for each overload independently, e.g.:

    delete(key : string): void; // or whatever you are returning
    delete(keys : string[]): void;
    // this is useful for calling inside another function with union parameter
    delete(keyOrKeys : string | string[]): void;
    
    delete(keyOrKeys : string | string[]){
       if(Array.isArray(keyOrKeys){
         // delete all keys in array
       } else {
         // delete single key.
       }
    }