This code snippet is encountering errors:
type Example = {
x: true,
y: null,
z: null
} | {
x: false,
y: Error,
z: null
} | {
x: false,
y: null,
z: { val: number}
}
function getExample(): Example {
return { x: false, y: null, z: { val: 5 } };
}
const { x, y, z } = getExample();
if (!x && !y) {
console.log(z.val)
}
The error shown is:
Object may be 'null'.(2531)
It seems logical as the types from the original Example
object were lost during destructuring of the result from getExample()
.
My attempt to emulate the behavior of the Apollo React client is reflected in the following code:
const { isLoading, isError, responseData } = useQuery('...');
if (isLoading) {
return '..';
}
if (isError) {
return '..';
}
return responseData.bar;
I am struggling to understand how they achieve this in their implementation.
Is it achievable in Typescript? Perhaps their typings are not perfectly accurate, and even when they don't, they always claim to return responseData
.