Passport JS Google OAuth20 Strategy Typescript Error

101 Views Asked by At

I'm currently migrating a passport JS Google OAuth2 strategy from JavaScript to TypeScript and I'm receiving the a TypeScript compile error. The clientID and ClientSecret fields are highlighted as having no overload matching the call. I've inspected the interface and it shows both options as string.

The full error is as follows:

TSError: ⨯ Unable to compile TypeScript: middleware/passport.google.ts:12:5 - error TS2769: No overload matches this call. The last overload gave the following error. Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'. Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.

An image of the issue can be seen here:

enter image description here

And the interface can be seen here:

enter image description here

Any ideas?

1

There are 1 best solutions below

0
Bry On

Ok, I figured this one out, just in case anyone else runs into the same issue. Unfortunately, dotenv is initially "undefined", so there are two possible solutions here:

  1. Set a default value for clientID and clientSecret by setting a default string. For example:

    const clientID: string = process.env.GOOGLE_CLIENT_ID ?? 'someDefaultString'  const clientSecret: string = process.env.GOOGLE_CLIENT_SECRET ?? 'someDefaultString'

  2. Edit the passport-google-oauth20 index.d.ts file (\node_modules@types\passport-google-oauth20\index.d.ts) as follows:

    clientID: string | undefined; clientSecret: string | undefined;

I personally used the first option as I prefer not to edit node modules directly.