I am working on nodemailer app with gmail OAuth2
in TypeScript, with "noImplicitAny": true,"noImplicitReturns": true
. This requires me to explicitly specify return types.
I have this snippet
import { google } from 'googleapis';
export const getOAuth2Client = (): OAuth2Client => { // 2. So I set the return type as OAuth2Client
const { OAuth2 } = google.auth;
const auth2Client = new OAuth2(
process.env.G_OAUTH_CLIENT_ID,
process.env.G_OAUTH_CLIENT_SECRET,
process.env.G_OAUTH_REDIRECT_URL
);
auth2Client.setCredentials({
/* eslint-disable @typescript-eslint/camelcase */
refresh_token: process.env.G_OAUTH_REFRESH_TOKEN,
});
return auth2Client; // 1. Inspecting auth2Client shows its of type OAuth2Client
};
I got the OAuth2Client
type on inspecting the type of the returned auth2Client
. The problem though is where to import it from and use it in the project.
I tried the following...
import { google, OAuth2Client } from 'googleapis';
That fails as googleapis
has no such named export.
I also saw this approach from @corolla answer where he imports the types from google-auth-library
import { OAuth2Client } from 'google-auth-library';
I would wish to use the type without having to add another library as a dependency just for a single type for an already typed library (Much as googleapis
uses types from google-auth-library
under the hood, the project linter requires me to list it as a project dependency, which I wouldn't wish to do for now). Is there any other way I can go about this please.
Thank you.
I settled with just disabling the eslint
warning for no-extraneous-dependencies
.
// eslint-disable-next-line import/no-extraneous-dependencies
import { OAuth2Client } from 'google-auth-library';
import { google } from 'googleapis';
I couldn't specifically specify google-auth-library
as a project dependency just to pick one type, yet under the hood, the googleapis
package was also importing the types for the same package. So I wouldn't be breaking anything I guess.