Currently, I am working on a project involving GraphQL. In my code, I have encountered a GraphQLError object with a property named extensions
. The type of this property is either void
or { [key: string]: any; }
. Whenever I try to access any property within extensions
, an error occurs:
x.extensions.code === 'UNAUTHENTICATED'
Property 'code' does not exist on type 'void | { [key: string]: any; }'.
Property 'code' does not exist on type 'void'.
To resolve the issue of accessing properties that do not exist in a union type, a potential solution can be found here: https://github.com/Microsoft/TypeScript/issues/12815#issuecomment-266193707
let pet = getSmallPet();
if ((<Fish>pet).swim) {
(<Fish>pet).swim();
}
else {
(<Bird>pet).fly();
}
However, the challenge lies in performing a type assertion without a specific type name.
If you have any insights on how I can address this dilemma, I would greatly appreciate it.
Best regards, Richard