Skip to content

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

Closed
5 tasks done
Pieter-1337 opened this issue Mar 18, 2022 · 8 comments
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@Pieter-1337
Copy link

Pieter-1337 commented Mar 18, 2022

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

@whzx5byb
Copy link

Duplicate of #467

@MartinJohns
Copy link
Contributor

This is one of the features that needs to be supported by JavaScript first.

@fatcerberus
Copy link

I don't use any workaround as there are none

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" });

@Pieter-1337
Copy link
Author

I don't use any workaround as there are none

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 :)

@fatcerberus
Copy link

For what it's worth, you don't need the cast as INamedParameters. You do need to include your default values in the interface, though: Playground

It'd be nice if you could write the types inline as function foo({ foo: string, bar?: number }) {}, but that doesn't work because it conflicts with the syntax for renaming destructured properties. 😢

@jcalz
Copy link
Contributor

jcalz commented 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.

@fatcerberus
Copy link

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.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Out of Scope This idea sits outside of the TypeScript language design constraints labels Mar 18, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Out of Scope" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

7 participants