English 中文(简体)
在Vue.js SFC档案中,在申报或使用TS类型/接口时发现电话错误
原标题:Getting intellisense errors when declaring or using TS types/interfaces in Vue.js SFC files

I m developing a component for my Nuxt 3 project. I m trying to declare an interface to provide strong typings for the component props, e.g.:

<script setup>
interface Props {
  float: number;
}

const props = defineProps<Props>();
</script>

然而,VS 在我试图这样做时,《刑法》显示错误:

 interface  declarations can only be used in TypeScript files. ts(8006)

我还注意到,我试图使用任何类型的说明(例如:let x:string):

Type annotations can only be used in TypeScript files. ts(8010)

I could > useîtime declaration syntax for the props - e.g:

const props = defineProps({
  float: { type: Number, required: true },
});

但是,我只使用Typegust syntax,因为我希望能够使整个项目的安全性和节点。

What I ve tried

我采取了标准步骤,使VS法典中的Vue案的Syntax能够:

  • I have Volar installed;
  • I also disabled the built-in TypeScript VS Code extension for the workspace in order to enable Takeover mode. The (takeover) label shows up in the status bar, indicating that it s working.

但它没有工作。 我试图重新启用Volar Vue服务器,甚至完全重新启用《科索沃法典》,但错误依然存在。 什么可能会造成错误出现?

问题回答

Argh, noob mis - I forgot to provide the lang="tsvide to the scripttag:

<script setup lang="ts">
interface Props {
  float: number;
}

const props = defineProps<Props>();
</script>

After adding lang="ts", the problem is gone.

The most frustrating thing is that nowhere it s hinted from VS Code that its absence could be the cause of the errors and it looks as if Volar is simply not taking over. I only noticed it after taking another look at the docs.





相关问题
store data in memory with nestjs

I am trying to persist some data in my nestjs server so I can then use that data in my client app through http requests. I have created a products.service.ts file with the function getAllData() that ...

React Hook Form Error on custom Input component

I am having a problem when I use react hook form + zod in my application, in short the inputs never change value and I get the following error in the console: Warning: Function components cannot be ...

Updatable promises using proxy in JavaScript

EDIT: I ve updated this question, and the old question is moved here. A POC can be found on this jsfiddle or the snippet below GOAL: The goal here is to make or rather simulate a promise that can be ...

热门标签