I'm encountering a scenario where I have many functions with similar parameters and struggling to find a solution to make everything easily maintainable. Take the following example:
public func1(param1: string, param2: string, param3: string){}
public func2(param1: string, param2: string, param3: string){}
public func3(param1: string, param2: string, param3: string){}
public func4(param1: string, param2: string, param3: string, param4: string){}
public func5(param1: string, param2: string, param3: string, param4: string){}
public func6(param1: string, param2: string, param3: string, param4: string){}
... Etc
Initially I was thinking about an interface such as:
interface IMyInterface {
param1: string;
param2: string;
param3: string;
}
and using it in the fuction:
public func1(args: IMyInaterface) {
}
But then in fuction 4,5,6 I can't really extend it to accept N number of additional args without creating new interfaces and I don't want to maintain all the derived interfaces.
I'd also like to keep my typings (not all parameters are strings and all functions have a defined number of parameters) so something like
func1(...args: string){}
wouldn't work
Any Idea how best to go about this?
func(newArg: boolean, param1: string, param2: string, param3: string){}) but clunky/messy to append arguments (func(param1: string, param2: string, param3: string, newArg: boolean){}), and I'm not sure ifNis something you need to be generic or what. Good luck!func1(...args: any[]){}? orfunc1(one:string,two:number,...rest:any[]){}, see typescriptlang.org/docs/handbook/functions.html#rest-parameters