DEV Community

Cover image for How to use named arguments in TypeScript functions?
👨‍💻Pierre-Henry ✨
👨‍💻Pierre-Henry ✨

Posted on Edited on

How to use named arguments in TypeScript functions?

If you are familiar with the keyword / named arguments in programming languages such as Python, Ruby and PHP 8, you probably wonder if the same can be achieved in TypeScript.

This is an excellent question! We are aware that named parameters / named arguments drastically help the readability of the code. With keyword arguments, we can directly tell what the mentioned argument does in the function call.

In TypeScript, we can use the object destructuring syntax on the function parameters. Just like this 👇

function invertSentence({
  sentence,
  doesFormat
}: {
  sentence: string;
  doesFormat: boolean;
}): string {
  if (doesFormat) {
    return sentence;
  }

  // logic ...

  // split into two segments
  const [stringOne, stringTwo] = sentence.split(
    ". "
  );

  // rebuild the sentence
  const rebuiltString = `${stringTwo} ${stringOne}`;

  return rebuiltString;
}
⭐ Must Read: pierre/named arguments function typescript nd - ประเด็นร้อน ⭐ Must Read: pierre/named arguments function typescript nd - ประเด็นร้อน

And then, you will be able to mention the required properties which will be the "named arguments".

invertSentence({ 
  sentence: 'TS makes my job easier. Writing clean code avoids me a headache',
  doesFormat: false
});
⭐ Must Read: pierre/named arguments function typescript nd - ประเด็นร้อน ⭐ Must Read: pierre/named arguments function typescript nd - ประเด็นร้อน

Conclusion

In conclusion, TypeScript doesn't offer named arguments natively like how PHP 8 does for instance.
However, thanks to the power of TypeScript's types and object destructuring, we can use types to force our functions' parameters to receive arguments with their respective property names, improving the readability of our code.


Top comments (3)

⭐ Must Read: pierre/named arguments function typescript nd - ประเด็นร้อน
 
pierre profile image
👨‍💻Pierre-Henry ✨

Let me know your thoughts and other interesting use-cases you might like to share? 😊

⭐ Must Read: pierre/named arguments function typescript nd - ประเด็นร้อน
 
Sloan, the sloth mascot
Comment deleted
⭐ Must Read: pierre/named arguments function typescript nd - ประเด็นร้อน
 
pierre profile image
👨‍💻Pierre-Henry ✨ • Edited

Hi Salmin! Sure, what don't you understand? Is is from the TS types?

If this is simpler for you, keep in mind that I could also have stored:

{
    sentence: string;
    doesFormat: boolean;
  }
⭐ Must Read: pierre/named arguments function typescript nd - ประเด็นร้อน ⭐ Must Read: pierre/named arguments function typescript nd - ประเด็นร้อน

into an interface like:

interface InvertSentence {
    sentence: string;
    doesFormat: boolean;
  }
⭐ Must Read: pierre/named arguments function typescript nd - ประเด็นร้อน ⭐ Must Read: pierre/named arguments function typescript nd - ประเด็นร้อน

and then, the function's signature would become

function invertSentence({
    sentence,
    doesFormat
  }: InvertSentence) {
⭐ Must Read: pierre/named arguments function typescript nd - ประเด็นร้อน ⭐ Must Read: pierre/named arguments function typescript nd - ประเด็นร้อน

Good luck!