title: Type-dependency between function arguments with function overloads in TypeScript
body: <p>I'm creating a factory function, which takes a type argument to indicate the type of object to create, plus an parameter argument with parameters describing the object, which is dependent on the type of the object. A simplified example would look like this:</p>
  <pre class="lang-js prettyprint-override"><code>enum EntityType {
    TABLE,
    BUCKET
  }
  type TableParams = { tableName: string }
  type BucketParams = { bucketName: string }
  
  function createEntity (type: EntityType.TABLE, params: TableParams): void
  function createEntity (type: EntityType.BUCKET, params: BucketParams): void
  function createEntity (type: EntityType, params: TableParams | BucketParams): void {
    switch(type) {
      case EntityType.TABLE:
        console.log('table name:', params.tableName)
        break
      case EntityType.BUCKET:
        console.log('bucket name:', params.bucketName)
        break
    }
  }
  </code></pre>
  <p>The function overloads ensure that users can only call the function with the right kind of parameters, depending on the entity type, e.g.</p>
  <pre class="lang-js prettyprint-override"><code>createEntity(EntityType.TABLE, { tableName: 'foo' })
  createEntity(EntityType.BUCKET, { bucketName: 'bar' })
  createEntity(EntityType.BUCKET, { tableName: 'fox' }) // =&gt; No overload matches this call.
  </code></pre>
  <p>The first two calls works just fine, but the 3rd call causes a compilation error: &quot;No overload matches this call&quot;.</p>
  <p>But how do I ensure type-safety <em>inside</em> the function? I.e. the example above doesn't actually compile because &quot;Property 'tableName' does not exist on type 'TableParams | BucketParams'.&quot;</p>
  <p>Shouldn't TypeScript be able to deduce the type of the <code>params</code> argument, based on which of the 2 function overloads matches?</p>