title: Is it possible to infer type of variable's property by it's another property value?
body: <p>I'm trying to make TypeScript automagically infer the type of variable by it's property value;</p>
  
  <p>Let's assume we have the following function</p>
  
  <pre class="lang-js prettyprint-override"><code>function ensureReturnTypeIsRight(...args: any[]): ReturnType {
      return Math.random() &gt; 0.5 ? {
          title: 'prop2',
          params: ['1', '2', '3'],
      } : {
          title: 'prop1',
          params: undefined,
      };
  }
  </code></pre>
  
  <p>And</p>
  
  <pre class="lang-js prettyprint-override"><code>type SomeMap = {
      'prop1': string,
      'prop2': (...args: string[]) =&gt; string,
  }
  
  type ReturnType = `
      Type that supposed to throw an error if title is not a key of SomeMap,
      to throw an error in case title is 'prop2' but no params were provided
      to throw an error in case title is 'prop1' but params were provided
      but not to throw an error in case everything is correct.
      When we try to assign a value to a variable of that type (in our case 'variable' is return value)
  `
  </code></pre>
  
  <p>It can be done with generic type like this</p>
  
  <pre class="lang-js prettyprint-override"><code>type ArgumentTypes&lt;F extends Function&gt; = F extends (...args: infer A) =&gt; any ? A : never;
  type GenericReturnType&lt;K keyof SomeMap = any&gt; = {
      title: K,
      params: SomeMap[K] extends Function ? ArgumentTypes&lt;SomeMap[K]&gt; : undefined,
  };
  </code></pre>
  
  <p>And function written like </p>
  
  <pre class="lang-js prettyprint-override"><code>function ensureReturnTypeIsRight(...args: any[]): GenericReturnType {
      const shouldHaveParams = Math.random() &gt; 0.5;
  
      if (shouldHaveParams) {
          const value: GenericReturnType&lt;'prop2'&gt; = {
              title: 'prop2',
              params: ['1', '2', '3'],
          };
  
          return value;
      }
  
      const value: GenericReturnType&lt;'prop1'&gt; = {
          title: 'prop1',
          params: undefined,
      };
  
      return value;
  }
  </code></pre>
  
  <p>So TypeScript will check if it's possible to assign a value of following type to a variable.
  But is it possible to do so without generics?</p>
  
  <p>I'm sorry for any unclear thoughts.</p>