Typescript object destructuring is caught by ESLint no-unused-vars rule

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?

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

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;
}

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.

Read More:   how to change an element type using jquery

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.


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts