Say i have a function like this one in typescript:
openMultipleAddFormModal(commission?: Commission, people?: People): void {
// some data
}
As of my understanding i added the Optional Chaining operator to make a parameter non necessary.
How do i change my code above to have something like this?
this.openMultipleAddFormModal(res); // where res here refers to people and NOT to commission
Can i do something like c# does, where you can provide the name of the parameter when calling the function and "binding" that to the property?
Something like this in C#:
// main method
public void UpdateBase(string machineRef = null, string userRef = null){
Ref = machineRef ?? Environment.MachineName;
UserRef = userRef ?? Environment.UserName;
}
// and then i specify the parameter i am passing
UpdateBase(userRef: userRef); // is there something like this in typescript / javascript?
Thanks for any help!
>Solution :
To "skip" the argument for an optional positional parameter in JavaScript and TypeScript, you pass undefined as the argument for the parameter:
openMultipleAddFormModal(undefined, peopleObject);
If you’re really into brevity, you could use void 0 instead of undefined (the void operator’s result is always undefined), but frankly it suffers in terms of readability…
There’s been discussion of allowing arguments to be simply skipped by writing fn(, second), but it’s never reached the point of progressing through the JavaScript proposal process.