title: typescript remove optional property
body: <p>I'm trying to build a dynamic type for a builder</p>
  
  <pre><code>export type Builder&lt;T, K extends keyof T&gt; = {
      [P in K]: (value: T[P]) =&gt; Builder&lt;T, K&gt;;
  } &amp; {
      build(): Readonly&lt;T&gt;;
  };
  </code></pre>
  
  <p>If I have a class or an interface with optional properties, I get this kind of error:</p>
  
  <pre><code>$ tsc
  test/OtherClass.ts:29:1 - error TS2722: Cannot invoke an object which is possibly 'undefined'.
  
  29 OtherClass.builder().patate(Patate.AU_FOUR).build();
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  </code></pre>
  
  <p>This is my class</p>
  
  <pre><code>export class OtherClass {
      constructor(
          public literal: string | undefined,
          public patate?: Patate,
          public hello: Readonly&lt;Hello&gt; = {},
      ) { }
  
      static builder: () =&gt; Builder&lt;OtherClass, keyof OtherClass&gt; = builder.for(OtherClass);
  }
  </code></pre>
  
  <p>I was expecting the type to create a builder with a non-optional method for each properties, but for some reason, the optionality of <code>patate</code> seem inherent to the key and not the type. I don't get this behavior with the property <code>literal</code> </p>
  
  <p>It looks like an issue to me. I'm using typescript 3.1.4. Is there another way to remove the question mark dynamically? </p>
  
  <p>I've tried to use the <code>NonNullable</code> helper to create first a copy of my type with nothing nullable, but <code>patate</code> remains optional. </p>
  
  <p>This is the effective type that vscode gives me</p>
  
  <pre><code>(property) patate?: ((value: Patate | undefined) =&gt; Builder&lt;OtherClass, "literal" | "patate" | "hello"&gt;) | undefined
  </code></pre>