title: Typescript: making interface parameters optional when passing to a function
body: <p>I have a typescript function, that accepts an options object as a parameter. I have an interface that defines the options. I also have a constant that contains default values for every option. It looks something like this:</p>
  
  <pre class="lang-js prettyprint-override"><code>interface Options {
    viewportHeight: number
    viewportWidth: number
    // ...
  }
  
  const defaults: Options = {
    viewportHeight: 600,
    viewportWidth: 600,
  // ...
  }
  
  function createGenerator(options: Options) {
    options = { ...defaults, ...options }
  
    return function generate() {
      // Simplfied
      return {
        value: Math.min(options.viewportWidth, options.viewportHeight),
      }
    }
  }
  </code></pre>
  
  <p>The problem is the following: if I leave all properties of the <code>Options</code> interface requires, then I can't do something like <code>createGenerator({ viewportWidth: 50 })</code> to set only the options I need and leave the rest by default</p>
  
  <p>If I do make them optional, then all options like <code>options.viewportWidth</code> have the type of <code>number | undefined</code> which causes an error <code>Argument of type 'number | undefined' is not assignable to parameter of type 'number'</code> when I'm trying to use them.</p>
  
  <p>I could define two interfaces like <code>Options</code> and <code>FilledOptions</code>, but that's not DRY. Is there some nice way around it?</p>