Assign parameter names to values when calling a function to avoid setting of unneeded optional parameters
See original GitHub issueSuggestion
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:
- Created 2 years ago
- Comments:7 (2 by maintainers)
Top GitHub Comments
Duplicate of https://github.com/microsoft/TypeScript/issues/467
You checked the box for
but this is definitely a runtime feature.