-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Assign parameter names to values when calling a function to avoid setting of unneeded optional parameters #48318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Duplicate of #467 |
This is one of the features that needs to be supported by JavaScript first. |
I mean, you can pass an object and destructure it, which is how this is usually done in vanilla JS too: interface FooOptions {
foo: string;
bar: string;
baz?: number;
}
function foo({ foo, bar, baz }: FooOptions) {
console.log(foo, bar, baz);
}
// prints "fooey, bard, undefined"
foo({ foo: "fooey", bar: "bard" }); |
@fatcerberus yeah I added that to the stackblitz and the issue just now! thx for the info though. Still feel it would be nice if we could just name the params like in C# for instance, oh well it'll do just fine with passing the object and destructuring it indeed :) |
For what it's worth, you don't need the cast It'd be nice if you could write the types inline as |
You checked the box for
but this is definitely a runtime feature. |
In theory it doesn’t have to be a runtime feature - but the alternative would be type-directed emit, which is also a no-go. |
This issue has been marked as "Out of Scope" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
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:
⭐ 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:
Or declared like this:
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
Workaround:
Pas an object as a param and destructure in the function call as shown below
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
The text was updated successfully, but these errors were encountered: