Skip to content

Allow multiple unused parameters to be call _ #24856

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
4 tasks done
RebeccaStevens opened this issue Jun 11, 2018 · 5 comments
Closed
4 tasks done

Allow multiple unused parameters to be call _ #24856

RebeccaStevens opened this issue Jun 11, 2018 · 5 comments
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints

Comments

@RebeccaStevens
Copy link

RebeccaStevens commented Jun 11, 2018

Search Terms

  • Duplicate identifier
  • Parameter
  • Underscore

Suggestion

When providing a callback method that must meet a certain signature, often not all parameters need to be used in the callback body. If only the 2nd parameter is used, the 1st still needs to be given a name; If only the 3rd, the 1st and 2nd still need names.

A common convention in many programming language is to use an underscore as the name of unused parameters.
For example in Haskell:

foo :: Int -> Int -> Int
foo 0 x = x
foo _ _ = 1

and in Prolog:

foo(0,X,X).
foo(Y,_,1) :- Y \= 0.

This can of course be done in typescript but only for 1 parameter as parameters aren't allow to have duplicate names. #9464 allowed unneeded parameters to be prefixed with an underscore to work nicely with noUnusedParameters; however those parameters still need to each have a unique name.

Using _1, _2, _3 etc seems clunky to me.
I would like to suggest that parameter named _ be made exempt from the Duplicate identifier error.

The compiler could simply switch out multiple uses of _ for _1, _2, _3 etc.

Use Cases

When I write functional JavaScript, I always use an underscore to represent an unused parameter. This suggestion would be nice for function where there are more than 1 parameters that aren't being used in the function body.

Examples

function foo(cb: (a: any, b: any, c: any) => void): void {
  // ...
  cb(x, y, z);
}

foo((_, _, c) => {
  console.log(c);
});

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. new expression-level syntax)
@ghost
Copy link

ghost commented Jun 11, 2018

The design goals don't permit us to change the semantics of JavaScript, and in JS (_, _) => 0 is a syntax error.

@ghost ghost added the Out of Scope This idea sits outside of the TypeScript language design constraints label Jun 11, 2018
@RebeccaStevens
Copy link
Author

@Andy-MS Oh? That's weird. I assumed that parameters of arrow functions worked the same way as with standard functions.

i.e. This isn't a syntax error:

function foo(_, _) {
  return 0;
}

My bad.

@ghost
Copy link

ghost commented Jun 12, 2018

Actually, that is an error in strict mode:

"use strict";
function foo(_, _) {} // SyntaxError: Duplicate parameter name not allowed in this context

@RebeccaStevens
Copy link
Author

RebeccaStevens commented Jun 12, 2018

Oops, my bad again.
I just chucked it into the console - I always forget to enable strict mode in the console 😛

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

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
Projects
None yet
Development

No branches or pull requests

2 participants