[英]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 .我正在使用useQuery和useResult获取一个简单的查询。
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
: result
和country
是ref
,因此要访问基础数据,请使用.value
展开它们:
const country = useResult(result, {}, (data) => data.country);
console.log(country.value.name);
👆
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.