title: Generic recursive type to extend a nested interface dynamically
body: <p>I have an object that contains the methods for all of the endpoints of my API. The nested structure of the object mimics the URL endpoints. For example (in reality it's a slightly more deeply nested object):</p>
  <pre><code>/api/users
  
  /api/recipes
  
  /api/recipes/user
  
  const api = {
      users: {
          get: await getUsers(),
          post: await postUser()
      },
      recipes: {
          get: await getRecipes(),
          post: await postRecipe(),
          user: {
              get: await getUserRecipes()
          }
      }
  }
  
  </code></pre>
  <p>I'm writing a Next JS app. I have two instances of the object shown above. One is for the back end, where each method makes a call to the database. The other is for the front end where each method makes a HTTP request to the back end.</p>
  <p>I am trying to keep my typings simple and not repeat myself across the codebase. In both instances of the object, the data returned is the same type for each method. That's obviously so, as the api.recipes.get method on the front end makes a http request that goes to a handler that then calls api.recipes.get on the back end.</p>
  <p>So, I have an interface that describes each method - the params they receive and  the data they return. Now, what I want to do is be able to use that interface to describe both instances of the api object, without having to write it out twice. Why? - because in reality the back end will return a API repsonse with a hasErrors property and either some errors or the data, whereas the front end instance will return a http response with the data in the data property.</p>
  <p>I'm hoping I can write a recursive type that will take the response type as a generic parameter and extend the API interface, changing the return type of each method from the base data type, to the given return type, with the base data as a property of it.</p>
  <p>I've had a go at writing such a type:</p>
  <pre><code>export type Unpromisify&lt;T&gt; = T extends PromiseLike&lt;infer U&gt; ? Unpromisify&lt;U&gt; : T;
  
  type Error = {}; // some error
  
  type HTTPResponse&lt;T&gt; = {
      status: number;
      data: T | Error[];
  }
  
  type APIResponse&lt;T&gt; = {
      hasErrors: true;
      errors: Error[];
  } | {
      hasErrors: false;
      body: T;
  }
  
  type HTTPMethod&lt;T extends ((...args: any) =&gt; Promise&lt;any&gt;)&gt; = (params: Parameters&lt;T&gt;[0]) =&gt; Promise&lt;HTTPResponse&lt;Unpromisify&lt;ReturnType&lt;T&gt;&gt;&gt;&gt;;
  
  type APIMethod&lt;T extends ((...args: any) =&gt; Promise&lt;any&gt;)&gt; = (params: Parameters&lt;T&gt;[0]) =&gt; Promise&lt;APIResponse&lt;Unpromisify&lt;ReturnType&lt;T&gt;&gt;&gt;&gt;;
  
  // EDIT - added missing types, thanks to jcalz, to make example reproducible
  
  interface UserParams { p: 1 }
  interface User { u: 2 }
  interface Recipe { r: 3 }
  interface RecipeParams { p: 4 }
  interface UserID { u: 5 }
  
  interface API {
      users: {
          get: (params: UserParams) =&gt; Promise&lt;User[]&gt;;
          post: (body: User) =&gt; Promise&lt;User&gt;;
      },
      recipes: {
          get: (params: RecipeParams) =&gt; Promise&lt;Recipe[]&gt;;
          post: (body: Recipe) =&gt; Promise&lt;Recipe&gt;;
          user: {
              get: (params: UserID) =&gt; Promise&lt;Recipe[]&gt;;
          }
      }
  }
  
  type APIExtension&lt;T extends ((...args: any) =&gt; Promise&lt;any&gt;) | {}, P extends 'API' | 'HTTP'&gt; = 
  T extends ((...args: any) =&gt; any) ? 
  P extends 'API' ? APIMethod&lt;T&gt; : HTTPMethod&lt;T&gt; :
  APIExtension&lt;T,P&gt;;
  
  type BackEndAPI = APIExtension&lt;API, 'API'&gt;;
  </code></pre>
  <p>I feel like I'm going in the right direction, but unfortunately on the last line I'm getting the error:</p>
  <pre><code>    Type instantiation is excessively deep and possibly infinite.ts(2589)
  </code></pre>
  <p>I understand this error is thrown if the type recursion exceeds the set threshold, but I can't work out why that's happening.</p>