Eslint is back and it's cracking down on unused variables with no

I've configured eslint to alert me about unused variables

rules: {
  '@typescript-eslint/no-unused-vars': ['error', { args: 'none' }],
}

Presently, I have a TypeScript class structured like this:

import { User } from './user';

export class MyClass {
  async myMethod(): Promise<User> {
    // Some code
  }
}

Upon running eslint, I encounter the following error message:

error  'User' is defined but never used  @typescript-eslint/no-unused-vars

I'm puzzled by eslint's warning. To me, it seems like the variable is being utilized.

Answer №1

When looking at the example provided, it's important to note that the type Promise is used to define a type and not necessarily indicate that User is being used in your Method. Eslint expectations are as follows:

import { IUser} from './user'; // interface for User object
import { User } from './user'; // User object

export class MyClass {
  async myMethod(User: string): Promise<IUser> {
    // Some code with User parameter
  }
}

In this case, your Eslint rule will only apply to the User object and not the interface.

For further information, you may refer to:

Answer №2

To prevent inaccurate error reporting, it is recommended to deactivate the base rule. For more information, please refer to: Deactivating Base Rule

  "no-unused-vars": "off",
  "@typescript-eslint/no-unused-vars": ["error", { args: "none" }]

UserModel.ts

export class UserModel {
    username: string;
}

MyComponent.ts

import { UserModel } from './UserModel'

export class MyComponent {
    async myFunction(): Promise<UserModel> {
        // Some code here
    }
}

Below is a simplified version of my eslint configuration

module.exports = {
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint/eslint-plugin"],
  "env": {
    "node": true
  },
  "extends": "eslint:recommended",
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module"
  },
  "rules": {
    "no-unused-vars": "off",
    "@typescript-eslint/no-unused-vars": ['error'],
  }
}

dependencies in package.json

{
  "scripts": {
    "lint": "eslint app.ts"
  },
  "devDependencies": {
    "@types/eslint": "7.2.4",
    "@types/node": "13.7.7",
    "@typescript-eslint/eslint-plugin": "4.14.1",
    "@typescript-eslint/parser": "4.14.1",
    "eslint": "7.18.0",
    "ts-node": "8.6.2",
    "typescript": "3.8.3"
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Querying data elements using Graphql mutations: a step-by-step guide

const MUTATION_QUERY = gql` mutation MUTATION_QUERY( $name: bigint! ) { insert_name( objects: { name: $name } ) { returning { id name } } } `; const [onClick, { error, data }] = useMut ...

Obtain non-numeric parameters from the URL in Angular 2 by subscribing to

How do I handle subscribing to a non-numeric parameter from a URL? Can the local variable inside my lambda function params => {} only be a number? Here's my code: getRecordDetail() { this.sub = this.activatedRoute.params.subscribe( ...

What is the process for defining a state using React Native and TypeScript?

Recently, I've embarked on my journey with React Native and decided to incorporate TypeScript into my development process. As I attempted to set my state, an error popped up that reads as follows: An issue arose while trying to assign the argument &a ...

When restarting the React application, CSS styles disappear from the page

While developing my React application, I encountered a problem with the CSS styling of the Select component from Material UI. Specifically, when I attempt to remove padding from the Select component, the padding is successfully removed. However, upon refre ...

How to perform a fetch on a local path in Next.js?

Is there a way to use the fetch method with a relative path like this: export async function getServerSideProps() { // Fetch data from local API const res = await fetch(`/api/get_all_prices`) const data = await res.json() // Pass data to th ...

What is the process for choosing nested colors from a theme in Material UI?

I have a question regarding how to select a nested style from my theme when creating a Button. Below is the snippet of code that illustrates my dilemma: const buttonStyle: SxProps<Theme> = { '&:hover': { backgroundColor: 'bac ...

What steps are involved in constructing Jodit from scratch?

Seeking a non-minified, readable version of Jodit, I attempted to build it myself. However, my lack of experience with composer, node, npm, webpack, TypeScript, and other tools has left me struggling. Is there anyone who can guide me through the process s ...

I'm baffled by how the community response is implemented in this particular TypeScript exercise on Exercism

I am currently learning TypeScript from scratch by working on exercises available on exercism Successfully completed the 5th exercise on Pangram. Below is my solution: class Pangram { alphabet = "abcdefghijklmnopqrstuvwxyz" constructor(privat ...

Instructions for setting 0 as a valid value in Html code and displaying it

I have a query regarding HTML code within an Angular app. My inquiry is, is there an alternative method to check for null or undefined values in an ngIf statement? The code I am working with looks like this: <div ngif= "value !== null and value ! ...

Finding out whether the current date falls between a startDate and endDate within a nested object in mongoose can be done by using a specific method

My data structure includes a nested object as shown: votingPeriod: {startDate: ISOdate(), endDate: ISOdate()}. Despite using the query below, I am getting an empty object back from my MongoDB. const organizations = await this.organizationRepository.find( ...

The Tools of the Trade: TypeScript Tooling

Trying out the amazing Breeze Typescript Entity Generator tool but encountering an error consistently. Error: Experiencing difficulty in locating the default implementation of the 'modelLibrary' interface. Options include 'ko', 'b ...

Encountering Issue: NG0303 - Unable to associate 'ng-If' as it is not recognized as a valid attribute of 'app-grocery'

NG0303: Encountering an issue with binding to 'ng-If' as it is not recognized as a valid property of 'app-grocery'. A similar problem was found here but did not provide a solution Despite importing CommonModule in app.modules.ts, I am ...

Modifying the website favicon based on the URL address

I am currently working with a client who wants our web application to reflect their branding through the URL. They have requested that we change the favicon on the page to display their private label logo, as well as changing the title. However, I am strug ...

"Modifying the form of an item by adjusting its variable, and rendering certain object properties as

let myObj = { a: { value: 1 }, b: { value: 2 } } myObj = { // How can I make the property b optional in myObj without specifying my own type? a: { value: 123 } } Is there a way to make the property myObj.b ...

Tips for sending props, state, or arguments to a TypeScript React component

Hey there, total newbie here... I'm currently working on a school project and I've hit a bit of a roadblock... I'm attempting to pass some props from one component to another, but for some reason, it's not working properly. The goal ...

TypeScript async function that returns a Promise using jQuery

Currently, I am facing a challenge in building an MVC controller in TypeScript as I am struggling to make my async method return a deferred promise. Here is the signature of my function: static async GetMatches(input: string, loc?: LatLng):JQueryPromise& ...

Employing distinct techniques for a union-typed variable in TypeScript

I'm currently in the process of converting a JavaScript library to TypeScript. One issue I've encountered is with a variable that can be either a boolean or an array. This variable cannot be separated into two different variables because it&apos ...

problem with arranging sequences in angular highcharts

I am facing an issue with sorting points in different series using highcharts. To illustrate my problem, consider the following example series: [ {name: 'series one', value: 5 values}, {name: 'series two', value: 10 values} ] When usin ...

The method of pausing a function until the result of another function is returned

There is a function named 'updateProfile()' that includes a condition, which checks for the value of variable 'emailChangeConfirm' obtained from another function called 'updateEmailAllProcessing()'. The issue lies in the fact ...

DotLottie file loading issues

While using dotlottie/react-player, webpack 4, and react 16, I encountered module parse failed errors during compilation. "@dotlottie/react-player": "^1.6.5" "webpack": "^4.44.2", "react": "16.14.0&qu ...