r/typescript 12d ago

node_modules problems

Running into an unexpected type error that I've never seen before with some unfortunately structured Google NPM libs. Should be easy to work around this issue with `as` or something similar but I'm curious if anyone has any ideas for a "cleaner" approach (that doesn't involve joining Google to fix their libraries).

The type error is this:

Type 'OAuth2Client' is not assignable to type 'string | GoogleAuth<AuthClient> | OAuth2Client | BaseExternalAccountClient | undefined'.

Type 'import("/Users/me/path/to/project/node_modules/google-auth-library/build/src/auth/oauth2client").OAuth2Client' is not assignable to type 'import("/Users/me/path/to/project/node_modules/googleapis-common/node_modules/google-auth-library/build/src/auth/oauth2client").OAuth2Client'.

Types have separate declarations of a private property 'redirectUri'.ts(2322)

Typescript is correct to call this out, but I don't see any way around it that doesn't involve type assertion.

0 Upvotes

5 comments sorted by

10

u/alexlafroscia 12d ago edited 12d ago

In my experience, this error crops up when there are two different versions of the same library being imported. Rather than fixing this in your source code with an assertion, I would leverage whatever your package manager provides for deduplicating dependencies or forcing one specific version to avoid both being in your `node_modules`

2

u/seniorsassycat 12d ago

npm explain googleapis-common

2

u/sickhippie 11d ago

Don't install googleapis-common directly.

Run npm ddp

https://docs.npmjs.com/cli/v12/commands/npm-dedupe

1

u/lucideer 11d ago

googleapis-common isn't installed directly, it's a transitive dependency of all the @googleapis libs. The issue is that google-auth-library is installed directly (needed for auth) & is also a tertiary transitive dep pinned to a specific older version (no semver range).

What I've done for now is to dedupe by downgrading the directly installed google-auth-library but that's an annoying step from a future maintenance perspective as it means this type error will inevitably re-emerge as Google update their transitive version pinning.