title: Create singleton from a library dependency
body: <p>I want to create a singleton of my own class which depends on google-auth-library.</p>
  <p>You can create an oauthClient like so:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import { OAuth2Client } from "google-auth-library";
  
  const OAuth2 = google.auth.OAuth2;
  
  const getOauthClient = (
    clientId: string,
    clientSecret: string,
    redirectUrl: string
  ): OAuth2Client =&gt; {
    return new OAuth2(clientId, clientSecret, redirectUrl);
  };</code></pre>
  </div>
  </div>
  </p>
  <p>Therefore, I'm trying to add this instance which is gonna be created if you call getOauthClient into my OauthClient class that looks like this:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>class OauthClient{
    constructor(
      clientId: string,
      clientSecret: string,
      redirectUrl: string
    ){
      if(OauthClient._instance){
        throw new Error("Cannot initialize singleton class using new");
      }
    }
  
    private static _instance: OauthClient = getOauthClient(clientId, clientSecret, redirectUrl);
  
    public static get instance(): OauthClient{
      return OauthClient._instance;
    }
  }</code></pre>
  </div>
  </div>
  </p>
  <p>How can I do it?</p>