Module

x/overload/mod.ts

A type-safe way to make overloads for your functions in Deno 🚀
Latest
File
export interface OverloadFunction { args: Function[]; func: Function;}
export function overload( name: string, ...functions: OverloadFunction[]): Function { return function (...args: any[]): any { const types = [];
for (const arg of args) { types.push(arg.constructor); }
for (const func of functions) { let isSame = true;
for (let i = 0; i < args.length; i++) { if (args[i].constructor != func.args[i]) { isSame = false; break; } }
if (isSame) { return func.func.apply(null, args); } }
throw new Error( `No overload of ${name} supports the arguments ${Deno.inspect(args)}`, ); };}