Import declaration conflicts with local declaration of ‘defineProps’ in Vue 3.3.

huangapple go评论57阅读模式
英文:

Import declaration conflicts with local declaration of 'defineProps' in Vue 3.3

问题

I started getting these errors after updating node_modules (and Vue to v3.3) just today.

Vue 3.3、WebPack(不是 Vite),以及 VS Code Volar 在使用。这个项目很大。

每个带有 <script setup lang="ts">*.vue 文件都有 import { defineProps, defineEmits } from 'vue'。之前它正常工作,现在它仍然可以编译并正常工作。Eslint 也成功通过。

但现在 VS Code 在每个文件中都突出显示导入的 definePropsdefineEmits。错误如下:

导入声明与本地声明的 'defineProps' 冲突.ts(2440)
别名function defineProps<PropNames extends string = string>(props: PropNames[]): { [K in keyof Readonly<{ [key in PropNames]?: any; }>]: Readonly<{ [key in PropNames]?: any; }>[K]; } (+2 overloads)
import defineProps
const defineProps: {
    <PropNames extends string = string>(props: PropNames[]): { [K in keyof Readonly<{ [key in PropNames]?: any; }>]: Readonly<{ [key in PropNames]?: any; }>[K]; };
    <PP extends ComponentObjectPropsOptions<...> = ComponentObjectPropsOptions<...>>(props: PP): { [K in keyof Readonly<...>]: Readonly<...>[K]; };
    <TypeProps>(): DefineProps<...>;
}

Import declaration conflicts with local declaration of ‘defineProps’ in Vue 3.3.

现在 VS Code 中的每个 *.vue 文件都被标记为红色,这非常烦人。

有什么办法可以解决这个问题吗?

英文:

I started getting these errors after updating node_modules (and Vue to v3.3) just today.

Vue 3.3, WebPack (not Vite), and VS Code Volar is in use. The project is huge.

Every *.vue file with <script setup lang="ts"> has import { defineProps, defineEmits } from 'vue'. It worked fine previously, it still compiles and works fine now. Eslint also passes successfully.

But now VS Code highlights imported defineProps and defineEmits in every file. The error is:

Import declaration conflicts with local declaration of 'defineProps'.ts(2440)
(alias) function defineProps<PropNames extends string = string>(props: PropNames[]): { [K in keyof Readonly<{ [key in PropNames]?: any; }>]: Readonly<{ [key in PropNames]?: any; }>[K]; } (+2 overloads)
import defineProps
const defineProps: {
    <PropNames extends string = string>(props: PropNames[]): { [K in keyof Readonly<{ [key in PropNames]?: any; }>]: Readonly<{ [key in PropNames]?: any; }>[K]; };
    <PP extends ComponentObjectPropsOptions<...> = ComponentObjectPropsOptions<...>>(props: PP): { [K in keyof Readonly<...>]: Readonly<...>[K]; };
    <TypeProps>(): DefineProps<...>;
}

Import declaration conflicts with local declaration of ‘defineProps’ in Vue 3.3.

Now every *.vue file is marked as red in VS Code which is very annoying.

Any idea where to look for to resolve the issue?

答案1

得分: 6

因为 'defineProps' 与编译器宏发生冲突。移除该导入。

如果构建无法正常工作,请确保您的 package.json 中有 "eslint-plugin-vue" 版本 8 及以上,并已安装。

"vue/setup-compiler-macros": true,

添加到 .eslintrc.js 文件的 env 部分。

英文:

It's because 'defineProps' conflics with compiler macros. Removes the import.

If the build does not work, make sure you have
"eslint-plugin-vue" version 8 and above in the package.json and installed.

Add

"vue/setup-compiler-macros": true,

to the env section of the .eslintrc.js file.

答案2

得分: 4

你不需要显式导入 defineProps,因为它是编译器宏,在 <script type="setup">(使用组合式 API)中会自动可用于您。

查看文档中的这个部分,解释了如何在单文件组件(SFC)中定义 props。

因为你在代码中导入了它,实际上在幕后做了两次,导致你看到的错误。移除导入,错误应该消失!

还要确保你使用了最新的 Volar 插件 for VS Code 并使用它的 takeover 模式,如官方文档的 TypeScript 部分所解释的。

英文:

You don't need to import defineProps explicitly because it is a compiler macro and is automatically available to you in &lt;script type=&quot;setup&quot;&gt; (using the composition api).

See this section in the docs, explaining how to define props in single file components (SFC)

Because you're importing it in your code, you're essentially doing it twice behind the scenes, which results in the error you're seeing. Remove the import and the error should go away!

Also make sure that you are using the latest Volar Plugin for VS Code and use it's takeover mode as explained in the TypeScript section of the official docs.

huangapple
  • 本文由 发表于 2023年5月17日 22:14:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定