title: Function parameter type based on value of another parameter
body: <p>I have TypeScript function, that has 2 parameters</p>
  <ul>
  <li>type: <code>&quot;user&quot; | &quot;book&quot;</code>,</li>
  <li>data: <code>User</code> if type is <code>&quot;user&quot;</code>, <code>Book</code> if type is <code>&quot;book&quot;</code>.</li>
  </ul>
  <p>Is there any way to get the correct <code>data</code> type based on value of <code>type</code> parameter without type assertion (<code>(data as Book).author</code>)? Here is <a href="https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgKoGdrIN7JHAWwgC5l0wpQBzAbmQF8AoUSWRFAIQHsuBrHPIRJkK1OnACuYABZcopcpRC0GjMAE8ADigCycTQBUtEdMgC8AiZnlprdAEY9epbnzpMN25Ee0A5IebIvBDqXDDIeobG6HSMjAhcIOTI0nAgACYANlgWADw%20KBAAHpAZpgX%20RAB8ABSewgUANMjpcGBwpJEF6ADaBQC6AJTmVTiMyBPIwOF1xuZmFgBEjnyLw9jjk1sJSVzZAHSZXFQ1re37kjJyg3QA9LfIAKJQUHLNZ3DICGnI9iiOMlsWDS6WQrn4EAAbhAQMgAO7AQE7dKI4CJZBwRzQ-abCZMehAA" rel="nofollow noreferrer">example</a>.</p>
  <pre><code>interface User { name: string; }
  interface Book { name: string; author: string; }
  type MapTypes = { user: User; book: Book; }
  type TypeName = keyof MapTypes; 
  
  const handler = &lt;Type extends TypeName&gt;(type: Type, data: MapTypes[Type]) =&gt; {
      if (type === &quot;book&quot;) {
          console.log(data.author); // Error, data is not typed as Book, but still as User | Book
      }
  }
  </code></pre>