title: Add fluent extension method to 3rd party class
body: <p>I'm using <code>zod</code> which allows to define some validation rules using fluent function.</p>
  <p>Especially, one of the functionis <code>optional</code> which allows to declare a field as optional. Unfortunately, this function does not accept a flag to enable/disable the optional behavior. In my case, the form is built dynamically and the optional flag must be defined at run time.</p>
  <p>I can create a small utility method to add this logic :</p>
  <pre class="lang-js prettyprint-override"><code>import { z, ZodType} from 'zod';
  
  const fieldIsRequired = true;
  
  // Method 1 : wrapper / Working
  const makeOptional = (input : ZodType, required : boolean): ZodType =&gt; required ? input : input.optional();
  
  const entry1 = makeOptional(
      z
      .string()
      .min(10)
      .max(100)
      , !fieldIsRequired
  );
  </code></pre>
  <p>This is working well, but I lost the <em>fluent</em> code. Having one rule is acceptable, but adding more rules will leads to a hamburger of function call and parameters</p>
  <p>How can I add a new fluent function to the zod type, which come from a 3rd party lib ?</p>
  <p>I've tried to implement extension methods, but I failed finding the correct syntax.</p>
  <p>Here's what I tried :</p>
  <pre class="lang-js prettyprint-override"><code>import { z, ZodType} from 'zod';
  
  const fieldIsRequired = true;
  
  // Method 2 : extension method / Not Working
  
  declare namespace zod {
      export abstract class ZodType {
          makeOptional: (required: boolean)=&gt; ZodType;
      }
  }
  // Add syntaxic sugar to the Zod schema
  ZodType.prototype.makeOptional = function (required: boolean): ZodType {
      return required ? this : this.optional();
  };
  
  const entry2 =   
    z
      .string()
      .min(10)
      .max(100)
      .makeOptional(!fieldIsRequired);
  </code></pre>
  <p>How to fix this ?</p>
  <p>Repro : <a href="https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgLwDRwFoQCYBUCeYApgL5wBmUEIcA5MjrQNwBQLAxhAHYDO85wIgBtsASR4AlIgEcArsChFscALxwYUWUVYsA9LrgBZIjAAWOOAEY4ALjgB3KAEMwxKHAMB1aAGtgXAHMObj44ECcfIgB5MBhgbichVTgACn8wWXg7LDxCInRFOQUlWzgAIwgIISInLgBKbJwCYlUAPjhC+UVlAH44dMzSgZgAOghY+K5ElLqdTl54Ii4NfGs1cMiYuIShFJY4A5R9w5G+KH8AmeODkZB-FMsABjrruFunAA8Hx+fX9ABCATCMSSGRdJQsWZsPQGYxmCwAJlKRA+MCWPEmYRM5mUBgAchB4N4oH5AmxsER2EInIo4FMQEQeGAnOwiCgLAhXijILA4E4ymcWfAqU4eDxME08ohXocwhFohMdnYUp1itg7BUqjV6ip2jlmtpXiQWMb9HAAILYZQ8fDLT7AdhwHiyAI09QQdSmNk5J3sL3hFj6vIjMBUGCE4MbBXbKZJNTkWRcdgx1Kq7oayrVWoNCW5Fqc2WKGCyKBcDpgtVwPpmYDiuw1nhjRWxmasEhzEKLZZQfBItQHY7IV6nDQXK6y273J4vCfhL5PX6z+VbSbTQGCETiKRFbqzIA" rel="nofollow noreferrer">TS playground</a></p>