简体   繁体   English

vue-apollo - 类型“只读”上不存在属性“值” <ref<readonly<any> &gt;&gt;' </ref<readonly<any>

[英]vue-apollo - Property 'value' does not exist on type 'Readonly<Ref<Readonly<any>>>'

THE SITUATION:情况:

I am trying to integrate vue-apollo v4 with Typescript.我正在尝试将 vue-apollo v4 与 Typescript 集成。

I am fetching a simple query using useQuery and useResult .我正在使用useQueryuseResult获取一个简单的查询。

useResult by default return this type: Readonly<Ref<Readonly<any>>> useResult 默认返回此类型: Readonly<Ref<Readonly<any>>>

THE CODE:编码:

import { GET_COUNTRY } from '../graphql/queries'
import { Country } from '../types'

setup() {
  const route = useRoute()
  const code = route.params.code

  const { result, loading } = useQuery(GET_COUNTRY, {code: code}, { 
    clientId: 'default', 
    fetchPolicy: 'cache-first'
  });
  const country = useResult(result, {}, (data) => data.country);

  console.log(country.name) // Property 'name' does not exist on type 'Readonly<Ref<Readonly<any>>>'.ts(2339)

  return {
    country,
    loading
  }
}

ATTEMPT 1:尝试 1:

const country: Country = useResult(result, {}, (data) => data.country);
// Type 'Readonly<Ref<Readonly<any>>>' is missing the following properties from type 'Country': native, phone, capital, currency, and 6 more.ts(2740)

ATTEMPT 2:尝试 2:

const country = useResult(result, {}, (data) => data.country as Country);
console.log(country.name) // Property 'name' does not exist on type 'Readonly<Ref<Readonly<any>>>'.ts(2339)

ATTEMPT 3:尝试 3:

const country: Country = useResult(result, {}, (data) => data.country as Country);
// Type 'Readonly<Ref<Readonly<Country | {}>>>' is missing the following properties from type 'Country': native, phone, capital, currency, and 6 more.ts(2740)

THE QUESTION:问题:

Is it possible to combine useResult with my own Typescript interface ?是否可以将useResult与我自己的 Typescript interface结合使用?

To type the result of useQuery() , specify it as a type argument:要输入useQuery()result ,请将其指定为类型参数:

const { result, loading } = useQuery<Country>(GET_COUNTRY, ⋯)
                                        👆

result and country are ref s, so to access the underlying data, unwrap them with .value : resultcountryref ,因此要访问基础数据,请使用.value展开它们:

const country = useResult(result, {}, (data) => data.country);
console.log(country.value.name);
                      👆

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型“CombinedVueInstance”上不存在属性“XXX” <Vue, {}, {}, {}, Readonly<Record<never, any> &gt;&gt;&#39; - Property 'XXX' does not exist on type 'CombinedVueInstance<Vue, {}, {}, {}, Readonly<Record<never, any>>>' 属性 &#39;google&#39; 不存在于类型 &#39;Readonly&lt;{ IGoogleMapTrackerProps }&gt; &amp; Readonly&lt;{ chirldren?: ReactNode; }&gt;&#39; - Property 'google' does not exist on type 'Readonly<{ IGoogleMapTrackerProps }> & Readonly<{ chirldren?: ReactNode; }>' 属性 &#39;XYZ&#39; 不存在于类型 &#39;Readonly&lt;{ children?: ReactNode; }&gt; 和只读&lt;{}&gt;&#39; - Property 'XYZ' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>' 类型“ Readonly &lt;{}&gt;”上不存在属性“ xxx” - Property 'xxx' does not exist on type 'Readonly<{}>' 类型“只读&lt;{}&gt;”上不存在属性“产品” - Property 'products' does not exist on type 'Readonly<{}>' 类型“IntrinsicAttributes 和 IntrinsicClassAttributes”上不存在属性“名称”<product> &amp; 只读&lt;{}&gt;'</product> - Property 'name' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Product> & Readonly<{}>' “只读”类型上不存在属性“路由器” <props & routecomponentprops<{}, staticcontext, poormansunknown> &gt;</props> - Property 'router' does not exist on type 'Readonly<Props & RouteComponentProps<{}, StaticContext, PoorMansUnknown>> 打字稿错误属性 x 在类型“只读”上不存在<Props> &amp; Readonly&lt;{ children?: ReactNode; }&gt;&#39; - Typescript error Property x does not exist on type 'Readonly<Props> & Readonly<{ children?: ReactNode; }>' this.state.someProp 在 ReadOnly 类型上不存在 - this.state.someProp Does Not Exist on type ReadOnly &#39;Readonly&lt;{}&gt; &amp; Readonly&lt;{ children?: ReactNode; 类型上不存在属性“导航” }&gt; - Property 'navigation' does not exists on type 'Readonly<{}> & Readonly<{ children?: ReactNode; }>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM