下面的代码出现了一个奇怪的错误:Property 'length' does not exist on type '{ [RefSymbol]: true; }'.
但是,当我在foo函数中检查getCount的类型时,我的IDE告诉我它的类型是Foo[]。我做错了什么?
import { defineStore } from 'pinia';
import { useStorage } from '@vueuse/core';
interface Foo {}
export const useFooStore = defineStore('foo', {
state: () => ({
foo: useStorage('foo', [] as Foo[]),
}),
actions: {
getCount() {
return this.foo.length; //Here's the error
},
},
});发布于 2022-02-18 01:45:48
我认为useStorage的返回值不是您所想的那样。我相信它返回一个ref类型,其中包含存储的实际数据的value属性。
尝试:
useStorage('jobs', [] as Job[]).value发布于 2022-02-18 00:20:37
现在,在你的帖子中没有太多的信息,但是根据错误信息说的,我猜如下所示。您正在使用以下类型定义对象
{ [RefSymbol]: true; }但是,由于在您的类型中不存在长度,我认为一个数组是期望的工作?
发布于 2022-02-18 10:24:05
当我在行前面加上错误时
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
return this.foo.length; //Here used to be the error然后它突然起作用了。它很丑,但很管用
https://stackoverflow.com/questions/71166628
复制相似问题