question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Assign parameter names to values when calling a function to avoid setting of unneeded optional parameters

See original GitHub issue

Suggestion

Parameter assignation when calling a function, to make code more readable and to omit optional parameters that would otherwise not need be defined in the function call

🔍 Search Terms

assign parameters calling function assignation optional omit

List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

⭐ Suggestion

When calling a function it is currently not possible to assign parameters to their defined param name in the calling function, this has as a result that when using optional parameters and you wish to use any optional parameter after the first optional parameter, that you would need to set a value to all optional parameter before the one you are setting.

This simply results in setting some optional parameters value equal to the values that were already set as either their default value or null in the function definition…

This is not very readable and causes clutter and is prone to error when multiple optional parameters are present on a function

📃 Motivating Example

Imagine following situation, we have a function with optional params So either declared like this:

function exampleFunc(x: string, y?: string, z?: string): void{ 
//Logic here does not matter for the feature
}

Or declared like this:

function exampleFunc(x: string, y: string = null, z: string = null): void{ 
//Logic here does not matter for the feature
}

Now if I wish to call this function and I only want to set the x and z parameters I am obliged to set the y param to it’s default value as shown below

exampleFunc("someValue", null, "someValue");

It would make more sense if I could call the function as stated below and omit the optional param I don’t want to change the default value for…

exampleFunc(x: "someValue", z: "someValue");

This would omit the need to set the y parameter to null since it already is null (or any other value that was set in the function definition) by default…

Stackblitz example: https://stackblitz.com/edit/typescript-ckfrdm

I feel this would greatly improve readability and omit code clutter

💻 Use Cases

I would like to be able to assign parameter names and set values to these names, in order to omit the setting of unneeded optional parameters.

Current shortcomings of this feature: => This feature not existing introduces the following shortcomings int TS code

  • code clutter,
  • less readable,
  • prone to error

Workaround:

Pas an object as a param and destructure in the function call as shown below

export interface INamedParameters {
  x: string;
  y?: string;
  z?: string;
}
function testFunc2({ x, y = null, z = null }: INamedParameters): void {
  if (!!y) {
    console.log('optional param y was passed in the params!');
  }
  if (!!z) {
    console.log('optional param z was passed in the params!');
  }
}

testFunc2({ x: 'test', z: 'someValue' } as INamedParameters);

As you can see x, y, z are settible in the object, yet not mandatory, in this manner we can simply pass an object of INamedParameters to our function with the properties that are relevant for the use case at hand

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
whzx5bybcommented, Mar 18, 2022
1reaction
jcalzcommented, Mar 18, 2022

You checked the box for

This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)

but this is definitely a runtime feature.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Named and Optional Arguments - C# Programming Guide
Named and optional arguments enable you to omit the argument for an optional parameter if you don't want to change the parameter's default...
Read more >
Is there a way to provide named parameters in a function call ...
In ES2015, parameter destructuring can be used to simulate named parameters. It would require the caller to pass an object, but you can...
Read more >
Effective Python: 4 Best Practices for Function Arguments
Accepting optional positional arguments (often called star args in reference to the conventional name for the parameter, *args) can make a ...
Read more >
Using Python Optional Arguments When Defining Functions
In this tutorial, you'll learn about Python optional arguments and how to define functions with default values. You'll also learn how to create...
Read more >
"Optional" should not be used for parameters
With an Optional parameter, you go from having 2 possible inputs: null and not-null, to three: null, non-null-without-value, and non-null-with-value.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found