Currently, I am developing a nodemailer app using Gmail OAuth2 in TypeScript. With the configuration options set to "noImplicitAny": true and "noImplicitReturns": true, I have to explicitly define return types.
Here is a snippet of my code:
import { google } from 'googleapis';
export const getOAuth2Client = (): 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({
refresh_token: process.env.G_OAUTH_REFRESH_TOKEN,
});
return auth2Client;
};
After inspecting the returned auth2Client
, I found that it has the type OAuth2Client
. However, I am facing issues when trying to import and use this type in my project.
I attempted the following...
import { google, OAuth2Client } from 'googleapis';
This failed as there is no named export for OAuth2Client
in googleapis.
Another approach I came across was shared by @corolla's answer, where they imported the types from google-auth-library
import { OAuth2Client } from 'google-auth-library';
My goal is to utilize the type without adding an extra library as a dependency just for a single type. Even though googleapis internally uses types from google-auth-library, my project linter requires listing it as a project dependency, which I prefer to avoid at the moment. Is there an alternative solution to address this issue? Your guidance would be greatly appreciated.
Thank you.