Possible issue with Apollo package types for ApolloError object

I am writing to report a possible issue with the Apollo package types. When using the ApolloError object, TypeScript allows me to access the extensions property directly, even when the graphQLErrors array may be empty, which can result in a “Cannot read property ‘extensions’ of undefined” error at runtime.

It is possible that the types defined in the Apollo package are inaccurate or incomplete, leading to this error. I suggest reviewing and updating the types to prevent this issue from occurring.

Example:

import { ApolloError } from "@apollo/client";

const handleError = (err: ApolloError) => {
  if (err.graphQLErrors[0].extensions?.code !== "CONFLICT") {
    console.log("Error occurred");
  }
};

Hi, thanks for the report!
That’s just how TypeScript works with arrays - you will see the same with this example:

const test: Array<{foo:string}> = []

test[0].foo

You can change that behavior by setting noUncheckedIndexedAccess: true in your tsconfig.json - but that’s really something you have to enable on a per-project level, we don’t have any influence on that on a library level.