The ongoing ESLint conundrum: Balancing between "Unused variable" and "Unknown type" errors when utilizing imports for type annotations

I've encountered a linting issue and I need some guidance on how to resolve it. Here's the scenario - when running

$ yarn lint -v
yarn run v1.22.4
$ eslint . -v
v6.8.0

With plugins vue and @typescript-eslint, I have the following code in a .ts file :

import Vue, { VNode } from 'vue';

export default Vue.extend ({
  render(createElement): VNode {
    return createElement('div',
      {},
      'Simple reproducible example'
    );
  }
});

The error message reads :

 error  in ./src/components/Demo.ts

Module Error (from ./node_modules/eslint-loader/index.js):

/full/path/to/src/components/Demo.ts
  1:15  error  'VNode' is defined but never used  no-unused-vars

Additionally, while serving the web app using yarn serve (vue-cli), the same error is displayed making it impossible to ignore. Removing the import statement for typehint does not solve the problem as evident by this new error :

ERROR in /full/path/to/src/components/Demo.ts(302,26):
4:26 Cannot find name 'VNode'.
    2 | 
    3 | export default Vue.extend ({
  > 4 |   render(createElement): VNode {
      |                          ^
    5 |     return createElement('div',
    6 |       {},
    7 |       'Simple reproducible example'

In order to avoid the previous issue, I removed the import which allows me to navigate the web app served by yarn serve. Any suggestions on how to properly address this error?

EDIT : .eslintrc.js file content

module.exports = {
  'env': {
    'browser': true,
    'es6': true,
    'node': true
  },
  'extends': [
    'eslint:recommended',
    'plugin:vue/essential',
    'plugin:@typescript-eslint/eslint-recommended'
  ],
  'globals': {
    'Atomics': 'readonly',
    'SharedArrayBuffer': 'readonly'
  },
  'parserOptions': {
    'ecmaVersion': 2018,
    'parser': '@typescript-eslint/parser',
    'sourceType': 'module'
  },
  'plugins': [
    'vue',
    '@typescript-eslint'
  ],
  'rules': {
    'indent': [
      'error',
      2
    ],
    'linebreak-style': [
      'error',
      'unix'
    ],
    'quotes': [
      'error',
      'single'
    ],
    'semi': [
      'error',
      'always'
    ]
  }
};

Answer №1

Ensure that you set up the parser for eslint to utilize @typescript-eslint/parser rather than espree. TypeScript code is interpreted differently from regular JavaScript, so not all linting rules that apply to JavaScript will be suitable for TypeScript.

The Configuration section of @typescript-eslint/typescript-eslint project offers a sample .eslintrc.js file to help you get started.

You can further enhance this configuration by including plugins for vue.

module.exports = {
  root: true,
  parser: '@typescript-eslint/parser',
  plugins: [
    '@typescript-eslint',
  ],
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/eslint-recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:vue/recommended',
    '@vue/standard',
    '@vue/typescript'
  ],
};

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

What is the most secure method to define options and retrieve their values in a type-safe manner?

I am currently utilizing a library that offers an interface with a great deal of flexibility. type Option = number | { x?: number; y?: number; z?: number; } interface Options { a?: Option; b?: Option; c?: Option; d?: Option; } function init ...

What is the best way to limit the character display to a maximum of 30 in Vue.js?

My current goal involves achieving the following task. In a certain variable, I currently have this text: let text = 'hey this is a super long phrase' I am looking to utilize a label to showcase the text, but with only a limited number of char ...

Learn how to implement a vertical bar chart in Highcharts by using it as a component and passing it as props

I recently imported a Bar Chart component from Highcharts, but it is being displayed horizontally. I would like to convert it into a vertical chart instead. Can someone please assist me in achieving this by passing the necessary props? <template v-slot ...

Vue event emissions are not triggered or detected

This is the functionality of the Child Component: registerUser() { if (this.$refs.form.validate()) { this.$emit('showLoader', true); // Triggers this.$fireAuth .createUserWithEmailAndPassword(this.email, this.password) .the ...

Having difficulty loading Angular2/ Tomcat resources, specifically the JS files

I am currently in the process of deploying my Angular2 web application on a Tomcat server. After running the ng build command, I have been generating a dist folder and uploading it to my Tomcat server. However, whenever I try to run my web app, I encounte ...

Using React Material UI to create multiple collapse components

Currently, I am facing an issue where all the collapses in my list are linked to one state for "open." This means that if I open one list, all the other lists also open. I am looking for a way to keep the collapses separate from each other without needing ...

Using Angular2 - How to pass the router parameter as a variable in ngForm

Struggling to pass a router param (id) to an ngForm and then to an event emitter. I am able to retrieve the id from the router successfully, but when trying to assign it to my singleOpenHome object, I encounter an undefined error: @Input() singleOpenHome: ...

What is the best way to ensure the font size is responsive in Laravel's welcome.blaze.php file?

On a large screen, the word "LaravelProject" and its links have ample space which I prefer. A newly created Laravel project appears differently on a small screen. When replacing "Laravel" with "LaravelProject," the wor ...

Is there a way to determine if a tuple is of infinite or finite length?

Is there a way to determine if a tuple is finite or infinite? I've been working on a solution, but it doesn't cover all cases: type IsFinite<T extends any[], Finite = true, Infinite = false> = T extends [] ? Finite : T extends (infer E ...

Vue Multiselect: Leveraging nested objects for custom options

I have been working with the Vue Multiselect Custom Option Template and I am trying to use the name property as customLabel. Typically, this would be a straightforward task by just using 'name', but in this scenario, the property I need is nested ...

How can I link the path field to an i18n variable in the Nuxt/Vue.js router? I attempted to do this, but I am having trouble accessing the i18n variable

import Vue from 'vue' import Router from 'vue-router' import homePage from '~/pages/index' Vue.use(Router) export function createRouter() { return new Router({ mode: 'history', routes: [ { ...

Handling Firebase 401 Token Expired/Refresh using Vue Axios Interceptor

Within a Vuejs v2 website, I have implemented interceptors to send a firebase token to my node backend. In the backend, I verify the token, fetch data using the user ID from a database, and handle API calls accordingly. Although I utilize onIdTokenChanged ...

How can interfaces be effectively integrated with node and mongoose?

I am working on a model interface where I need to fetch specific data from the record // file: code.interface.ts import { Document } from 'mongoose'; export interface CodeI extends Document { readonly _id: string; readonly logs: any; } Howe ...

What is the best way to iterate through a collection of two or more arrays in order to determine the total length of all

https://i.stack.imgur.com/PpFlB.pngI currently have multiple Arrays containing various inputs this.listNumber = [ { "GenericQuestions": [ { "input": "long", }, { "input": & ...

What are the steps for implementing a heatmap in Vue Highcharts?

Struggling with error 17 while attempting to use a heatmap chart type in vue-highcharts. I've searched online and it appears that importing and correctly using it may solve the issue. Can someone guide me through this process as there seems to be no d ...

Tips for utilizing a primary template within a secondary container

In my code snippet below, I have outlined two steps: "step===1" and "step===2". My goal is to reuse the step 1 template for step 2. This means that after completing step 1 and clicking on the next button, I want to show the same template again for reusabil ...

A guide on integrating a stacked bar chart from ApexCharts into a modal or v-dialog component

I'm facing a minor issue with vue-apexcharts while attempting to showcase some data in a stacked bar chart. Here is the simple component I have created: <template> <div v-if="this.loaded"> <apexchart w ...

Displaying images dynamically in React from a source that is not public

There are 3 image options being imported, determined by the value in the state which dictates which of the 3 to display. import csv from '../../images/csv.svg'; import jpg from '../../images/jpg.svg'; import png from '../../images/ ...

Compiling an Angular project with an external library in AOT mode using angular-cli is causing issues and not compiling successfully

Embarking on a fresh new project, I utilized angular-cli 8.1.2 for the generation process. The goal is to establish a shared library that caters to multiple microservices (apps). This particular library should remain separate from the applications folder, ...

Angular's error notification system seems to be lacking in providing accurate

I'm experiencing an issue with my angular app where errors are not displayed properly. Instead of showing errors in the component and line number, they always appear in main.js. This is different from how errors are displayed in my other angular appli ...