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

Variable used in 'key' appears as unused #329

Closed
Kruptein opened this issue Jul 24, 2021 · 19 comments
Closed

Variable used in 'key' appears as unused #329

Kruptein opened this issue Jul 24, 2021 · 19 comments
Labels
bug Something isn't working upstream

Comments

@Kruptein
Copy link

The below template gives a "'i' is declared but its value is never read." in both volar and vue-tsc.

<template v-for="[i, part] of someObject" :key="i">
  {{ part }}
</template>

While the i is indeed not used inside the template itself, it is used in the key and can thus not simply be removed.

A temporary workaround can be:

<template v-for="something of someObject" :key="something[0]">
  {{ something[1] }}
</template>
@johnsoncodehk johnsoncodehk added the bug Something isn't working label Jul 24, 2021
@johnsoncodehk
Copy link
Member

Vue template AST doesn't provide key binding from ForNode so we can't fix it yet.

@bartenra
Copy link

Sorry for posting a duplicate!

How can I tell Volar to ignore an issue in a template?

@johnsoncodehk
Copy link
Member

@bartenra you can use _index instead of index as variable name.

@Zehir
Copy link

Zehir commented Sep 20, 2022

Vue template AST doesn't provide key binding from ForNode so we can't fix it yet.

Is there any changes about this issue in the last year ? Thanks

@johnsoncodehk
Copy link
Member

@Zehir unfortunately we have nothing can do here, you can open an issue in vue/core repo.

@Kruptein
Copy link
Author

Kruptein commented Sep 21, 2022

This actually nowadays works properly in my setup. Unsure if I should close the issue or not given that apparently Zehir still has issues.

@Zehir
Copy link

Zehir commented Sep 21, 2022

By using a _ prefix to your variables names ?

@Kruptein
Copy link
Author

No, with the proper names.

I just tested the following quickly:

<div v-for="[i, layer] of layers" :key="i">
    {{ layer }}
</div>

and this does not complain. If I remove the key section it properly complains that i is not used.

@Zehir
Copy link

Zehir commented Sep 21, 2022

I have 2 project, in one of them I don't have the issue.
What is the type definition of layers ?

@Kruptein
Copy link
Author

Kruptein commented Sep 21, 2022

In this case an array of strings.

ComputedRef<LayerName[]> to be precise, with LayerName being a string enum

@johnsoncodehk
Copy link
Member

I just tested the following quickly:

<div v-for="[i, layer] of layers" :key="i">
    {{ layer }}
</div>

This problem is only happen with <template> tag but not html element / component tag.

@Kruptein
Copy link
Author

This problem is only happen with <template> tag but not html element / component tag.

Aaah right, my bad, the initial PR is quite a while ago, so I hastily assumed that it was for all iterations :D

Sorry for the confusion Zehir :)

@matthew-dean
Copy link

Just ran into this. What's weird is Volar actually knows / infers the type of the item in the collection, so it knows its being used, but also doesn't know its being used.

      <template
        v-for="item in value"
        :key="item.id"
      >

In the above, item autocompletes with id

@hl037
Copy link

hl037 commented Mar 5, 2024

Any news since last year ? (still getting the error)

@paescuj
Copy link

paescuj commented Mar 5, 2024

Any news since last year ? (still getting the error)

I think #329 (comment) is still the case.

@GRyanParoz
Copy link

Any news since last year ? (still getting the error)

I think #329 (comment) is still the case.

Is there a relevant issue for Vue regarding this? It's been nearly 3 years, seems like a long time for an issue to exist, albeit one that just causes problems with static analysis tools.

@so1ve
Copy link
Member

so1ve commented May 29, 2024

This issue is now resolved, doesn't it work for you? @GRyanParoz

@GRyanParoz
Copy link

This issue is now resolved, doesn't it work for you? @GRyanParoz

Sorry, I misread the above update. Disregard!

@vincerubinetti
Copy link

vincerubinetti commented Jul 24, 2024

Sorry, I'm a little confused at the discussion above. Is this fixed across the board? I'm still getting a slight warning of an unused variable:

Screenshot 2024-07-23 at 9 08 57 PM

(Note the faded color of index in the v-for)

vue-tsc runs fine, so not an issue there. That screenshot is saying the warning is coming from "ts"... so I'm not sure which part of the language tools that's coming from... I guess the language server and ts plugin?

tsconfig.json
{
  "extends": ["@vue/tsconfig/tsconfig.json"],
  "include": ["src/**/*", "./*", "src/**/*.json"],
  "compilerOptions": {
    "composite": true,
    "esModuleInterop": true,
    "lib": ["ESNext", "DOM", "DOM.Iterable"],
    "noEmit": true,
    "noErrorTruncation": true,
    "noUncheckedIndexedAccess": true,
    "paths": {
      "@/*": ["./src/*"]
    },
    "resolveJsonModule": true,
    "strict": true,
    "target": "ESNext"
  }
}
package.json
{
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vue-tsc --noEmit && vite build",
    "preview": "vite preview",
    "lint": "eslint . --ext .vue,.ts --fix && prettier --write \"**/*.{vue,ts,css,scss,md}\"",
    "test:spelling": "bunx cspell src/**/*.{vue,ts}"
  },
  "dependencies": {
    "@fortawesome/fontawesome-svg-core": "^6.6.0",
    "@fortawesome/free-brands-svg-icons": "^6.6.0",
    "@fortawesome/free-regular-svg-icons": "^6.6.0",
    "@fortawesome/free-solid-svg-icons": "^6.6.0",
    "@fortawesome/vue-fontawesome": "^3.0.8",
    "@headlessui-float/vue": "^0.14.0",
    "@headlessui/vue": "^1.7.22",
    "@turf/turf": "^7.0.0",
    "@vue/eslint-config-typescript": "^13.0.0",
    "@vueuse/core": "^10.11.0",
    "d3": "^7.9.0",
    "dom-to-image": "^2.6.0",
    "leaflet": "^1.9.4",
    "leaflet-providers": "^2.0.0",
    "lodash": "^4.17.21",
    "radix-vue": "^1.9.2",
    "vue": "^3.4.33",
    "vue-gtag": "^2.0.1",
    "vue-router": "^4.4.0",
    "vue-tippy": "^6.4.4"
  },
  "devDependencies": {
    "@ianvs/prettier-plugin-sort-imports": "^4.3.1",
    "@types/d3": "^7.4.3",
    "@types/dom-to-image": "^2.6.7",
    "@types/geojson": "^7946.0.14",
    "@types/leaflet": "^1.9.12",
    "@types/lodash": "^4.17.7",
    "@vitejs/plugin-vue": "^5.0.5",
    "@vue/eslint-config-prettier": "^9.0.0",
    "@vue/tsconfig": "^0.5.1",
    "eslint": "^8.57.0",
    "eslint-plugin-vue": "^9.27.0",
    "eslint-plugin-vuejs-accessibility": "^2.4.0",
    "postcss": "^8.4.39",
    "prettier": "^3.3.3",
    "prettier-plugin-css-order": "^2.1.2",
    "prettier-plugin-jsdoc": "^1.3.0",
    "sass": "^1.77.8",
    "typescript": "^5.5.3",
    "vite": "^5.3.4",
    "vue-tsc": "^2.0.28"
  }
}
VSCode details

VSCode: v1.91.1
Vue Official extension: v2.0.10
ESLint extension: v3.0.10

Another thing I just noticed is that hovering over :key="index" doesn't show a type for index (or _index, doesn't matter the name). Actually, no popup shows at all.


EDIT Nevermind, I guess I was several patch versions behind in my vscode extension (thought I had auto-update on). Updated, and now it's working fine!

vincerubinetti added a commit to colorado-cancer-center/ecco that referenced this issue Jul 24, 2024
- fix unit of Public Water System Violations
- use `cent_lat` and `cent_long` that have apparently always been in the
backend, instead of using turf.js to calc center of mass for label
positions
- rearrange api funcs a bit
- ~move `index` -> `_index` to avoid [this volar
issue](vuejs/language-tools#329
- try to make AppMap component more re-usable/generic
- remove popup content. replace with slot and make popup content the
responsibility of consuming/parent component.
   - make prop types generic instead of being tied to api functions
   - tweak feature info popup data handling
- move "onMoveEnd" map handler to top level so its debouncing can be
cancelled on unmount
- remove "map.remove" call because it is already getting called before
map mount (and calling it there [throws an
error](Leaflet/Leaflet#4806) for some
reason... leaflet just doesn't play well with modern web frameworks)
- for positioning feature labels, get info right from feature properties
field
- tweak feature label appearance
- move tooltip that shows unit to just one place, the top left legend
- move feature popup content to SectionMap page component, with some
typing (FeatureInfo)
- change logic of when certain stats (total/min/max/avg/etc) show or not
- add some util ts types
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working upstream
Projects
None yet
Development

No branches or pull requests

10 participants