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.

Revisiting _ underscore for unused parameters: create `unused` type

See original GitHub issue

Search Terms

_, underscore, noUnusedParameters, --noUnusedParameters, tslint, ts-lint, #9458, palantir/tslint#3442

Suggestion

Create an unused type for function parameters that are not used.

Use Cases

Can be used to clearly state unused parameters, while still playing nice with --noUnusedParameters and linters enforcing variable name conventions.

May also help in cases such as

  • #14154 - destructuring will fail at runtime if argument is not an object
  • #18420 - type Foo<usused> = { } ?

Examples

1. Unused parameter

function typicalNodeCallback(err: unused, data: any) {
  // do stuff with `data`
}

2. Using unused parameter is an error

function expressMiddleware(req: unused, res: any) {
  const body = req.body // ERROR!
}

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, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:39
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

32reactions
ajafffcommented, Dec 30, 2018

I don’t think adding a type solves anything. It actually loses type information: Sometimes you need to declare the type of a parameter even if you don’t use is. For example in methods of a base class that are meant to be overridden.

30reactions
mjacksoncommented, Jul 21, 2020

At the very least, a function parameter should not be considered to be “unused” if a parameter that comes after it is used. In that case, the “unused” parameter is actually serving a purpose by taking up a slot in the argument list.

function fun(a, b) {
  // The `a` parameter should not be considered "unused"
  console.log(b);
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Michael Jackson on Twitter: "TypeScript experts: is there a ...
Revisiting _ underscore for unused parameters: create `unused` type ... Create an unused type for function parameters that are not used.
Read more >
typescript - Skip type check on unused parameters
Using the underscore trick is still very useful in Typescript. Like I mentioned in my original answer say if you was using express...
Read more >
Underscores for unused parameters? : r/learnjavascript - Reddit
In Visual Studio Code (JavaScript code or TypeScript code), an unused parameter is displayed in a different color – unless its name starts...
Read more >
Ignore fist parameter of function if unused with a simple
If you ever find yourself in a situation where you need to implement function receiving multiple parameters, but you don't really need all ......
Read more >
About the unused-argument syntax in R2009b
For another example, suppose you write a function that has to take three input arguments (because another function is always going to pass ......
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