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
- 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
})
}
- Execute
tsc
for Typescript compile
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 14
- Comments: 45 (16 by maintainers)
@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@goodbomb in the JS sample we know given the context and usage,
loadSavedCredentialsIfExist
can only beOAuth2Client
. TS doesn’t have this context and only knows it’s aJSONClient
(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
andin
narrowing rather thanas
, e.g.:@danieltroger
The former returned an
AuthClient
while the current change is returning aGoogleAuth
instance. TheGoogleAuth
instance can contain a variety ofAuthClient
s (OAuth2Client
,JWT
,UserRefreshClient
, etc.). In that case, either instance should work.I will happily pass along this feedback - I think we can improve documentation for bundled packages as well.
Yup. Works perfect! Thanks
I would recommend installing v8.9.0 for now (
npm i google-auth-library@8.9.0
) untilgoogleapis
andgoogleapis-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.jsonI’ll sync with the team and get this resolved for you.