Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to provide only second parameter to function typescript?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 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…

Playground link

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading