question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Imports used exclusively in the `<template>` part of Vue SFCs are considered "never used" by Volar

See original GitHub issue

This issue was originally raised in simonhaenisch/prettier-plugin-organize-imports, a library which sorts imports and removes unused ones.

_Originally posted by @simonhaenisch in https://github.com/simonhaenisch/prettier-plugin-organize-imports/issues/71#issuecomment-1224104071_:

[simonhaenisch/prettier-plugin-organize-imports] is using @volar/vue-typescript as a drop-in replacement to create the language service, and after that it’s just a matter of calling the organizeImports method.

It seems that imports exclusively used in the <template> part of Vue SFCs are incorrectly considered as “never used” by Volar. Here is a minimal working example:

package.json:

...
    "devDependencies": {
        "@volar/vue-typescript": "^0.40.1",
        "prettier": "^2.7.1",
        "prettier-plugin-organize-imports": "^3.1.0",
...

types.ts:

export enum ThemeColor {
    PRIMARY = "primary",
    SECONDARY = "secondary",
    ACCENT = "accent",
    ERROR = "error",
    INFO = "info",
    SUCCESS = "success",
    WARNING = "warning"
}

sfc.vue:

<script setup lang="ts">
import { ThemeColor } from "@/types"
</script>

<template>
    Color is: {{ ThemeColor.PRIMARY }}
</template>

import { ThemeColor } from "@/types" is incorrectly considered as “never used” by Volar and then removed by simonhaenisch/prettier-plugin-organize-imports. If I add const color = ThemeColor.PRIMARY to the <script setup> part of sfc.vue, the import is not considered “never used” by Volar.

The same happens with functions used exclusively in the <template> part of Vue SFCs:

helpers.ts:

export function helloWorld(): string {
    return "Hello Worlds"
}

sfc.vue:

<script setup lang="ts">
import { helloWorld } from "@/helpers"
</script>

<template>
    {{ helloWorld() }}
</template>

Here, import { helloWorld } from "@/helpers" is marked as “never used” by Volar. However, if I add const hello = helloWorld() to the <script setup> part, the import is kept untouched.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
simensolcommented, Aug 24, 2022

Same isse with

<script setup lang="ts">
import { ThemeColor } from "../utilities/types"
</script>

<template>Color is: {{ ThemeColor.PRIMARY }}</template>
0reactions
johnsoncodehkcommented, Aug 27, 2022

@simonhaenisch it’s v0.40.2 because there are no breaking changes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Breaking component import. "... is declared but its value is ...
Only when importing components. ... Breaking component import. ... <template> part of Vue SFCs are considered "never used" by Volar #1757.
Read more >
Using Vue with TypeScript - Vue.js
Volar is the official VSCode extension that provides TypeScript support inside Vue SFCs, along with many other great features. TIP. Volar replaces Vetur,...
Read more >
How to avoid "Component is declared but its value is never ...
I'm using it and it is really great! UPDATE: And with Vue 3 as the New Default, Volar is recommended in official Vue...
Read more >
Common mistakes to avoid while working with Vue.js
The first thing to do in order to use Vue.js is to import it. If you follow the official guide and use an...
Read more >
Vue 3: Support new <script setup> without ref sugar - YouTrack
It looks like plugin already supports most of new syntax, only issues I found are: when component is imported is and used in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found