title: Typescript "in" operator fails to work in nested interface
body: <p>I have the following dumbed down example (<a href="https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgEJwM4QMpjpAFQgzGQG8AoZZAI0wiJIC5kAiYU4DZOW+5EgHsoKDhAC2rANwUAvhQqhIsRClzDijUpWoIArkPEBGFjurIYgwaarnqdKCxB7xNaDLuyPyL7aEiASUhjGztxOFAWdCxcfAZiMG9qLARBEAATOCgATyj6WMIE719ksA0giQAmUPNw0Gq0fLxCkiSBCFSMrOyG6Jxm+NbbX3kKTpIBMpESBJZ1aa1kAF5yW31DE1W7S2stuz5HZABOI4AaW2pZc8vryfLgzbNqOpBTPiwtFnZObl46LDuImQYkkPluyQ6aUyOTe-0GYC+HGBv3eKH8omCrDBw1u6Iq4gaT2QL0JqM+bCRXB4qMBGIkWKuF3anWhPVh9HJ32R1LhtOBmOxNzkMgoMD0IAQYGAaWQkBIBAwAB4CEZkBAAB6QDLcADWEGyghgyHmmgSAD4ABS2OVgBUAQSgAHMWCrbp1IJqWHqDUaTRgtABtFUAXQoAEo9uNSPooFB8ctaTMSAGbfancGRbo0hMY1AAMJpD0QdLxla5-EB90asAZ2zAI0W1hwrRY0DIXMF8Aa4v4iNEgD0-eBIGQAAtoBiBKPBHoADbpWgoXhgbIABxQYFH+DVmog2tl48aMQGiyU0HgSCZ40Es4gADpZ4JHRaO4XuyXgmG2tfbw+ny+9FjTsiw-CQ72bBIv2QQdkAAUQAJXggB5eDhlsEQwEAkdXy7dUe2CGR5CjZAMKwgA1OBZwTVMMEbPFgkqVhTjYElWC-MZsxve9H2fUioBACjZ3AjlIKkaCh03KkqUEHVmLcBA4AMFACGwZAdRAQQAHduE00dgAQUd+QkfluQ1RAwFnbIKCAA" rel="nofollow noreferrer">link</a>):</p>
  <pre><code>interface BaseStateTest {
    baseTest: &quot;it is a base store item&quot;;
  }
  
  interface StoresTest {
    custom1: {
      foo: {
        bar: number;
      };
    };
    storeItem1: {
      main: BaseStateTest;
      secondary: BaseStateTest;
    };
    storeItem2: {
      main2: BaseStateTest;
      secondary2: BaseStateTest;
    };
  }
  
  const storestest: StoresTest = {
    custom1: {
      foo: {
        bar: 99,
      },
    },
    storeItem1: {
      main: { baseTest: &quot;it is a base store item&quot; },
      secondary: { baseTest: &quot;it is a base store item&quot; },
    },
    storeItem2: {
      main2: { baseTest: &quot;it is a base store item&quot; },
      secondary2: { baseTest: &quot;it is a base store item&quot; },
    },
  };
  
  function testTs&lt;T1 extends keyof StoresTest&gt;(
    testTsArg: T1,
    context: keyof StoresTest[T1]
  ) {
    const currItem = storestest[testTsArg];
  
    const currContexedItem = currItem[context];
    if (&quot;baseTest&quot; in currContexedItem) {
      // in here it should be a type that extends the BaseStateTest interface
      console.log(currContexedItem);
      console.log(currContexedItem.baseTest); // ERROR
    }
    return currContexedItem;
  }
  const returnVal = testTs(&quot;storeItem2&quot;, &quot;main2&quot;);
  console.log(returnVal.baseTest); // this is ok, because TS knows which item it is exactly
  </code></pre>
  <p>I'm curious on how I can get TypeScript type inference to work on the line where I want to log:</p>
  <p><code>console.log(currContexedItem.baseTest); // ERROR</code></p>
  <p>My goal is to identify the type of a deeply nested object using the TypeScript <code>in</code> operator.</p>
  <p>Unfortunately I cannot seem to be able to accomplish it without changing the data structure to a less nested state. I am greeted with a TS error instead:</p>
  <pre><code>Property 'baseTest' does not exist on type '{ abc: number; abc2: { test: number; }; storeItem1: { main: BaseStateTest; secondary: BaseStateTest; }; storeItem2: { main2: BaseStateTest; secondary2: BaseStateTest; }; }[T1][keyof { ...; }[T1]]'.(2339)
  </code></pre>
  <p>Is there any existing solution that allows me to identify the type inside a function of such a nested object <strong>without</strong> changing the data structure?</p>