title: constructor parameter type as optional lookup property
body: <p>i've just started creating constructors based on my interfaces and i'm stuck here, where &quot;Property 'message' does not exist on type '{ message: string; } | undefined'&quot; in the errorMessage parameter's lookup type:</p>
  <pre class="lang-ts prettyprint-override"><code>interface fetchResponse {
    data:null|object|any[];
    error?:{
      message:string;
    };
  };
  interface FetchResponse extends fetchResponse {};
  class FetchResponse {
    constructor(data:fetchResponse[&quot;data&quot;], errorMessage?:fetchResponse[&quot;error&quot;][&quot;message&quot;]) {
      this.data = data;
      this.error = errorMessage ? {
        message: errorMessage,
      } : undefined;
    };
  };
  </code></pre>
  <p>i found the following possible ways to fix this, but i've been able to successfully implement none:</p>
  <ol>
  <li>resolving the property of <code>error</code> based on the property of <code>data</code> (i.e if <code>data</code> is an object/array then <code>error</code> is undefined, and if <code>data</code> is null then <code>error</code> and <code>error.message</code> are defined), which <em>should</em> make it so that <code>error</code> is not possibly undefined if i'm trying to pass an <code>errorMessage</code>: the only way i found to maybe do that is with a union (<a href="https://stackoverflow.com/questions/41705559/dynamically-resolve-property-type-based-on-another-property-value-in-typescript">according to this SO post</a>), but imitating that as seen below only caused typescript to flag 2 more problems which i'm clueless about in the latter half of the code</li>
  </ol>
  <pre class="lang-ts prettyprint-override"><code>interface fetchResponsePositive {
    data:object|any[];
  };
  interface fetchResponseNegative {
    data:null;
    error: {
      message:string;
    };
  };
  
  type fetchResponse = fetchResponsePositive | fetchResponseNegative;
  </code></pre>
  <ol start="2">
  <li><p>overloading (<a href="https://stackoverflow.com/questions/67152641/typescript-property-type-lookup-doesnt-work-with-optional-key">according to this OS post</a>)? this is my first ever exposure to the concept of overloading, after looking it up i understand the concept but cant figure out how to implement it here in a way that solves the issue (or whether it's even a solution to this)</p>
  </li>
  <li><p>adding a definite assertion to the <code>fetchResponse[&quot;error&quot;][&quot;message&quot;]</code> lookup: i just have no idea how to do that, i placed the exclamation mark in every conceivable spot to no success</p>
  </li>
  </ol>