Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Request no-any rule to ignore rest args #4437

Closed
jednano opened this issue Jan 4, 2019 · 7 comments
Closed

Request no-any rule to ignore rest args #4437

jednano opened this issue Jan 4, 2019 · 7 comments

Comments

@jednano
Copy link

jednano commented Jan 4, 2019

Feature request

Is your feature request related to a problem? Please describe.
Using the no-any rule, I get frustrated when destructuring ...rest args:

    public emit(eventName: string, ...rest: any[]) {

In this case, the emit method is going to call an internal listener with listener(...rest). I don't care what the types are. I just want to pass-through the ...rest args to the listener. I run into this issue quite frequently.

It would be great if I didn't have to scatter // tslint:disable-next-line:no-any everywhere I need ...rest args.

Describe the solution you'd like

    "no-any": [true, "ignore-rest-args"],

Describe alternatives you've considered
I considered using generics, but it's too limiting:

    public emit<T>(eventName: string, ...rest: T[]) {

That would force the ...rest args to have all of the same type. If the first ...rest arg was a string, it would force all the rest to be string as well.

Additional context

@jednano jednano changed the title Request no-any rule to ignore rest args to be of type any[] Request no-any rule to ignore rest args Jan 4, 2019
@JoshuaKGoldberg
Copy link
Contributor

Hi @jedmao! TS 3 added variadic rest arg types, so in theory you should be able to work around the restriction of ...rest: T[]:

type Func<T extends any[]> = (...args: T) => void;

let listener: Func<[string, number]> = (one: string, two: number) => { /* ... */ };

Does that solve your use case?

@jednano
Copy link
Author

jednano commented Jan 6, 2019

@JoshuaKGoldberg I'm not sure how that solves the problem, as it still uses any[] on your first line. Also, it requires knowledge of [string, number], which I really don't have. A listener could truly have any signature. Also, my listener example is just the most recent one I ran into. I have this problem all the time w/ the no-any rule. I prefer to not use any where I can help it, but there are some truly unavoidable scenarios. The ...rest args are the easiest one that comes to mind. There might be others.

@jednano
Copy link
Author

jednano commented Jan 9, 2019

It looks like the following might work:

    public emit(eventName: string, ...rest: Array<unknown>) {

@jednano jednano closed this as completed Jan 9, 2019
@jednano
Copy link
Author

jednano commented Jan 9, 2019

I take that back. This is still an issue.

@jednano jednano reopened this Jan 9, 2019
@adidahiya
Copy link
Contributor

I agree, I think this rule should be lenient (by default) about ...rest args

@vmk1vmk
Copy link
Contributor

vmk1vmk commented Mar 17, 2019

Hi, I am new to the open source community and this is my first PR. I thought this would be a good first issue to work on and I would love to receive some feedback.

@JoshuaKGoldberg
Copy link
Contributor

Fixed super cleanly in #4581! 💪

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants