overarg
A tool to help with returning typed arguments from overloaded functions in TypeScript.
What it does
Consider an overloaded method that can take 0-3 arguments in any combination that looks like this...
public testname: string: void;public testage: number: void;public testdob: Date: void;public testname: string, age: number: void;public testname: string, dob: Date: void;public testage: number, dob: Date: void;public testname: string, age: number, dob: Date: void;public test: void
You might have to write a bunch of logic to figure out what parameters you have, or you could do this inside the function:
;;;
Each variables will be strong-typed or undefined, easy as can be. You can even have multiple arguments of the same type and specify an offset so for instance:
// 1st string provided is firstName;// 2nd string provided is lastName;
Installation
npm install --save overarg
Usage
As shown in the example above, define all your overloads, but the actual function signature doesn't need any arguments.
// include in your project; // offset defaults to 0, so the first match for the identifier will be used; // specify the offset as a number (indexed from 0) to get later matches (ex. offset = 1, gets the second match);
type is the return desired return type, ex. string or RegExp, but the actual return overarg() will be that type | undefined (in case it couldn't match). This also allows you to set default values easily:
// sets variable1 to the value of the first argument that was a number or 100 if there wasn't one;
Identifiers
The identifiers in this list are tested in this order:
-
"array" - Will match an argument if it is an Array.
-
"object", "boolean", "function", "number", "string", "symbol", "undefined" - Will match an argument if typeof returns the specified primitive JavaScript type.
-
string - Will match an argument that has the specified name in its prototype chain. For example, when MyClass extends MyParent, you can specify "MyClass" or "MyParent" as the identifier.
-
object - Will match an argument that is an instanceof the specified object. For example, /myRegExp/g is an instanceof RegExp.
It is important to understand what typeof and instanceof do, and how they are resolved by JavaScript, some odd examples:
typeof new Date; // is objecttypeof null; // is objecttypeof undefined; // is undefined
Functions
When the above identifiers are not enough, you can use a Function to find the argument, by using overargFunc():
;
An example:
// test case showing monkey is not matched, but dog is;;assert.strictEqualresult1, undefined;;;assert.strictEqualresult2, dog;
Arrow Functions
Please note that JavaScript Arrow Functions do not have their own arguments https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions, but you can use ...args: any[] in TypeScript even on an Arrow Function and it will generate proper JavaScript.
In that case, you should pass ...args instead of ...arguments.
test"testing";
Real-World Example
// list the blobs via a streaming patternpublic list: ReadableStream<azs.BlobService.BlobResult, Out>;public listprefix: string: ReadableStream<azs.BlobService.BlobResult, Out>;public listtransform: StreamTransform<azs.BlobService.BlobResult, Out>: ReadableStream<azs.BlobService.BlobResult, Out>;public listoptions: StreamOptions<azs.BlobService.BlobResult, Out>: ReadableStream<azs.BlobService.BlobResult, Out>;public listprefix: string, options: StreamOptions<azs.BlobService.BlobResult, Out>: ReadableStream<azs.BlobService.BlobResult, Out>;public listprefix: string, transform: StreamTransform<azs.BlobService.BlobResult, Out>: ReadableStream<azs.BlobService.BlobResult, Out>;public listtransform: StreamTransform<azs.BlobService.BlobResult, Out>, options: StreamOptions<azs.BlobService.BlobResult, Out>: ReadableStream<azs.BlobService.BlobResult, Out>;public listprefix: string, transform: StreamTransform<azs.BlobService.BlobResult, Out>, options: StreamOptions<azs.BlobService.BlobResult, Out>: ReadableStream<azs.BlobService.BlobResult, Out>;public list: ReadableStream<azs.BlobService.BlobResult, Out>
This module addresses the "get arguments" section, making it look like this:
// get arguments ; ; ; if transform options.transform = transform;