Skip to content
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

noUnusedParameters and rest parameters #18653

Closed
jamiewinder opened this issue Sep 21, 2017 · 3 comments
Closed

noUnusedParameters and rest parameters #18653

jamiewinder opened this issue Sep 21, 2017 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@jamiewinder
Copy link

TypeScript Version: 2.5.2

There are some situations where you need to destructure a property from an object in order to exclude it from the rest:

Code

function test(
    { a, ...rest }: { a: number, b: number, c: number},
    fn: (params: { b: number, c: number}) => void
) {
    fn(rest);
}

(Real life example: extract a prop in React so you can forward the rest to the inner component)

However, if I do this with noUnusedParameters, I get an error ('a' is unused). I'd argue that in this particular scenario, it's not unused as such because it's necessary to isolate it from the rest of the parameters.

I can do the following to workaround the problem:

function test(
    { a: _, ...rest }: { a: number, b: number, c: number},
    fn: (params: { b: number, c: number}) => void
) {
    fn(rest);
}

But I thought I'd raise this regardless.

@ghost
Copy link

ghost commented Sep 21, 2017

IMO the workaround makes for better code, because with --noUnusedParameters on you can assume that any identifier gets used unless intentionally marked otherwise.
You should also just be able to do function test(x: { a, b, c }, fn: (params: {b, c}) => void) { fn(x) } in the usual case due to subtyping.

@DanielRosenwasser
Copy link
Member

Related is #12766.

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Nov 21, 2017
@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.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants