title: TypeScript Builder Pattern similar to Java
body: <p>I'm trying to implement the Builder Patten similar to Java in TypeScript 4.6.2.
  From what I have read, this isn't the preferred way to do it, but I have some constrains regarding the module exports.</p>
  <pre><code>export class HttpRequest {
  
      static Builder = class {
  
          private body: string;
  
          setBody(body: string): HttpRequest.Builder {
              this.body = body;
              return this;
          }
  
          build(): HttpRequest {
              return new HttpRequest(this);
          }
  
      }
  
      private readonly body: string;
  
      constructor(builder: any) {
          this.body = builder.body;
      }
  
      getBody(): string {
          return this.body;
      }
  
  }
  </code></pre>
  <p>It seems that the <code>setBody</code> method can't return a <code>HttpRequest.Builder</code> type, with the following error:</p>
  <p><code>TS2702: 'HttpRequest' only refers to a type, but is being used as a namespace here.</code></p>
  <p>It seems to work fine if <code>// @ts-ignore</code> is present before the method declaration, or if the method signature is changed to <code>setBody(body: string): any</code>.</p>
  <p>Is there any way to implement this with nested classes? I'm trying to avoid having two different classes, one for <code>HttpRequest</code> and one for <code>Builder</code>.</p>