使用:v-for指令中的键属性时TS的预期类型错误
尝试用键保持状态代码> v-for 指令,但Typescript在上抛出以下错误:key
属性。
TS错误
(property) key?: string | number | symbol
Type '{ toString: (radix?: number) => string; toFixed: (fractionDigits?: number) => string; toExponential: (fractionDigits?: number) => string; toPrecision: (precision?: number) => string; valueOf: () => number; toLocaleString: { ...; }; }' is not assignable to type 'string | number | symbol'.ts(2322)
runtime-dom.d.ts(1480, 3): The expected type comes from property 'key' which is declared here on type 'ElementAttrs<LiHTMLAttributes>'
代码
<script lang="ts" setup>
import { ref } from 'vue';
interface Workspace {
id: Number
name: String
}
const workspaceList = ref<Workspace[]>([])
</script>
<template>
<div>
<ul>
<li v-for="workspace in workspaceList" :key="workspace.id">
{{ workspace.id }}: {{ workspace.name }}
</li>
</ul>
</div>
</template>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能没有在
workspace
接口成员中使用意图的类型。您可能需要原语,而不是其对象对应物(请注意小写):参考日常类型:
It's likely that you are not using the types that you intended in your
Workspace
interface members. You probably want primitives rather than their object counterparts (note the lowercase):Reference the TS documentation for everyday types: