61

I have an object in Typescript that I am destructuring in order to extract a partial object. However, it fails my linter check:

async someFunction(username: string): Promise<UserDTO> {
    const userEntity = await getUserByUsernameAsync(username);

    if (userEntity ) {
        const { password, ...result } = userEntity ;
        return result;
    }

    return null;
}

As you can see, the above code grabs an object and strips out some parts of the object that we don't want to return, and returns the rest of the object.

However, the linter gives a warning:

warning  'password' is assigned a value but never used      @typescript-eslint/no-unused-vars

The object destructuring is assigning passport to a value and result to another object value and passport is the one that isn't being used. How do I fix this issue in order to pass the linter?

4 Answers 4

99

You can disable this verification for rest siblings adding "@typescript-eslint/no-unused-vars": ["error", { "ignoreRestSiblings": true }] to your list of rules in eslintrc.js.

Example:

module.exports = {
  root: true,
  parser: "@typescript-eslint/parser",
  plugins: [
    "@typescript-eslint",
  ],
  extends: [
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended"
  ],
  rules: {
    "@typescript-eslint/no-unused-vars": ["error", { "ignoreRestSiblings": true }]
  },
  settings: {
    react: {
      version: "detect"
    }
  }
};

You can also opt for disabling the linting rule for that line altogether adding this to the line above it:

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Sign up to request clarification or add additional context in comments.

4 Comments

For javascript and eslint use: "no-unused-vars": ["error", { "ignoreRestSiblings": true }]
I couldn't get this to work with "@typescript-eslint/no-unused-vars": ["error", { "varsIgnorePattern": "_", "ignoreRestSiblings": true }] ... do I have the syntax wrong?
@escapecharacter you may need the argsIgnorePattern if you're trying to allow arguments to have underscores eslint.org/docs/rules/no-unused-vars#argsignorepattern
This is not working for me either, in a spec file with typescript. I have "@typescript-eslint/no-unused-vars": ["warn", { argsIgnorePattern: "^_", varsIgnorePattern: "^_", ignoreRestSiblings: true }] in eslintrc.js Eslint detects the line as error: const { coordinates: _, ...inputWithoutCoordinates } = input;
24

It seems now days you will need these two rules added to your .eslintrc.json.

argsIgnorePattern will allow underscore arguments in your function signatures, while varsIgnorePattern will allow underscores in destructuring.

We're using the pattern: ^_ to make sure the variable name starts with an underscore.

Example

"rules": {
  "no-unused-vars": ["error", {
    "varsIgnorePattern": "^_",
    "argsIgnorePattern": "^_"
  }]
}

Documentation

Notes

In the original question it is sufficient to utilize: "ignoreRestSiblings": true because it is in use with "rest siblings".

I would still recommend using an underscore prefix as an explicit notation. It's also worth noting that without the rest pattern, ignoreRestSiblings will not solve the problem when trying to destructure and utilizing underscore notation.

1 Comment

varsIgnorePattern is exactly what I was looking for!
8

You can look to either remove the linter setting using the ignoreRestSiblings or pass the entire object and then look to delete the property.

async someFunction(username: string): Promise<UserDTO> {
    const userEntity = await getUserByUsernameAsync(username);

    if (userEntity ) {
        const {...result} = userEntity;
        delete result.password;
        return result
    }
    
    return null;
}

3 Comments

result is a shallow copy of userEntity and your approach will change the userEntity too
@farzad no it won't, the password property will be removed only from the shallow copy: replit.com/@fabiob/QuietJumpyGnudebugger
If you destructure password it will be removed from result and then you don't have to use delete. I.e. const { password, ...result } = userEntity;
-1

You can do:

const { field1: _, field2: __, ...rest } = yourObject;

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.