0

Suppose i have something like this:

createSettings(a = null, b = null, c = null, d = 50) {
}

I would really like to avoid calling my functions like this:

createSettings(null, null, null, 60)

I know one way is to accept an object instead of a multiple parameters:

createSettings(settings) {
}

And then i would call it in this way:

createSettings({d: 60})  

But the problem with this approach is that i lose the advantage of knowing what i should pass to the function!, meaning the function only says pass a setting, it doesn't say the setting can accept a, b, c, d !, which is kinda important because it helps me understand how to use a function, and my functions usually are complicated and knowing what to pass is really important as well.

So here is my question, is there a way to combine these two approaches and have a function:

createSettings(a = null, b = null, c = null, d = 50) {
}

And then called it like this?:

createSettings(...{d: 50})  

Any solution or suggestion is much appreciated.

Ali Ahmadi
  • 701
  • 6
  • 27

1 Answers1

1

Although those are optional parameters your function will still need some kind of identifier in order for it to know the index of the parameters but apparently an alternative to this is making use of an interface as your parameter. In your case, it would be something like:

 export interface setting
{
a?: any;
b?: any;
c?: any;
d?: any;
}

export function myFunction(params: setting){   
}


myFunction({a: 2, d: 5})

reference: How to pass optional parameters while omitting some other optional parameters?

Nemavhidi
  • 108
  • 8