google-auth-library-nodejs: `GoogleAuth` can no longer be used with `googleapis`: Type 'GoogleAuth' is not assignable to type 'string | OAuth2Client | BaseExternalAccountClient | GoogleAuth | undefined'

After updating google-auth-library-nodejs from v7.14.1 to v8.0.1, the following Typescript compile error occurs:

Type 'GoogleAuth<JSONClient>' is not assignable to type 'string | OAuth2Client | BaseExternalAccountClient | GoogleAuth<JSONClient> | undefined'.
  Type 'import(".../node_modules/google-auth-library/build/src/auth/googleauth").GoogleAuth<import(".../node_modules/google-auth-library/build/src/auth/googleauth").JSONClient>' is not assignable to type 'import(".../node_modules/google-gax/node_modules/google-auth-library/build/src/auth/googleauth").GoogleAuth<import(".../node_modules/google-gax/node_modules/google-auth-library/build/src/auth/googleauth").JSONClient>'.
    Types have separate declarations of a private property 'checkIsGCE'.

Environment details

  • OS: Windows
  • Node.js version: v16.14.2
  • npm version: v8.7.0
  • google-auth-library version: v8.0.1

Steps to reproduce

  1. Use the same code as previously
import { google } from 'googleapis'
import { GoogleAuth } from 'google-auth-library'

function setCredentials(): void {
  const client = new GoogleAuth({
    scopes: ['https://www.googleapis.com/auth/cloud-billing', 'https://www.googleapis.com/auth/cloud-platform'],
  })

  google.options({
    auth: client, // Error is emmitted for this line after updating google-auth-library-nodejs to v8
  })
}
  1. Execute tsc for Typescript compile

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 14
  • Comments: 45 (16 by maintainers)

Most upvoted comments

@LearningLadoo this can usually be solved when all dependent libraries are up-to-date.

Spent way more time on this than I wanted to so have to share

So trying to change to 8.9.0 didn’t work for me at all because my project was already pointed to that 😕

So I had to figure this out the hard way. A strategically placed Exclude with the right imports makes everything work


import { google, Auth } from 'googleapis';
import { JSONClient } from 'google-auth-library/build/src/auth/googleauth';
import { Impersonated } from 'google-auth-library';

/**
 * Reads previously authorized credentials from the save file.
 *
 */
async function loadSavedCredentialsIfExist(){
  try {
    const content = await fs.readFile(TOKEN_PATH);
    const credentials = JSON.parse(content.toString());
    return google.auth.fromJSON(credentials) as Exclude<Impersonated, Auth.GoogleAuth<JSONClient>>;
  } catch (err) {
    return null;
  }
}

@goodbomb in the JS sample we know given the context and usage, loadSavedCredentialsIfExist can only be OAuth2Client. TS doesn’t have this context and only knows it’s a JSONClient (which can be a number of clients). We have a few options for telling TS that this is definitely a particular type:

Personally, I like instanceof and in narrowing rather than as, e.g.:

const client = auth.fromJSON(credentials)
if ("refreshAccessToken" in client) {
  return client; // this is definitely an `OAuth2Client`
}

@danieltroger

But I think it was something with my code, I did the following change and it fixed the type error

screenshot

Idk what I’m doing tho and if the code still works, but I’ll see if it fails in the future (or maybe you know?)

The former returned an AuthClient while the current change is returning a GoogleAuth instance. The GoogleAuth instance can contain a variety of AuthClients (OAuth2Client, JWT, UserRefreshClient, etc.). In that case, either instance should work.

Small feedback: I checked here (because it’s linked here) if there was any breaking change in how api should be specified and could not find what I was look for. It seems like you have some auto-generated “breaking change” thing which seems very noisy to someone like me who just wonders how to fix a type error that shows up, maybe it could be improved? Screenshot 2023-07-31 at 17 12 40

I will happily pass along this feedback - I think we can improve documentation for bundled packages as well.

I would recommend installing v8.9.0 for now (npm i google-auth-library@8.9.0) until googleapis and googleapis-common are updated.

Yup. Works perfect! Thanks

I would recommend installing v8.9.0 for now (npm i google-auth-library@8.9.0) until googleapis and googleapis-common are updated.

Thanks a lot @danielbankhead! Much appreciated!

Looking a little closer, I see we have a publishing issue - @googleapis/youtube is a bit out of date on the registry: https://github.com/googleapis/google-api-nodejs-client/blob/main/src/apis/youtube/package.json

I’ll sync with the team and get this resolved for you.