【问题标题】:Object is of type 'unknown'.Vetur(2571) when populating a constant in TypeScript using Apollo使用 Apollo 在 TypeScript 中填充常量时,对象的类型为“未知”.Vetur(2571)
【发布时间】:2021-12-04 22:05:18
【问题描述】:

我正在使用 useQuery(来自 Apollo)来获取数据,如下所示:

const {
  result: fetchedResources,
  loading: fetchResourcesLoading,
  refetch: fetchResources,
  onError: apiError
} = useQuery(GET_RESOURCES, { limit: numberOfResults.value })

我在 v-for 中使用 fetchedResources 来循环数据:

  <router-link
    v-for="resource in fetchedResources.resources"
    :key="resource.id"
    class="resource q-mb-md items-center text-black"
    tag="div"
  >
...

每当我使用 resource 时,我都会在 Visual Studio 中收到以下错误:

Object is of type 'unknown'.Vetur(2571)

我认为问题是我需要在常量中为每个填充变量声明类型。任何想法如何解决这个问题?

【问题讨论】:

  • fetchedResources的类型是什么?您是否可以将类型参数传递给useQuery,以便正确输入fetchedResources
  • 我在 Quasar 文档 (quasar.dev) 或其存储库 (github.com/quasarframework/quasar) 中没有看到 useQuery
  • fetchedResources 是一个数组,我相信 useQuery 是 Apollo 的一个功能。

标签: typescript vue.js apollo quasar-framework vetur


【解决方案1】:

看起来 Vetur 最近发布了 0.35.0,它在 Apollo 的 Typescript 代码中发现了一个真正的错误。这些文档提供了有关如何为每个数组设置类型的信息:https://www.apollographql.com/docs/react/development-testing/static-typing/

【讨论】:

    猜你喜欢
    • 2021-04-12
    • 2020-08-23
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多