title: Type for extracting nested object keys
body: <p>Let's say I have know type for some part of data and nested data structure which looks like:</p>
  
  <pre><code>interface Element {
      foo: string;
      bar: string;
  }
  
  interface Elements {
      [name: string]: Partial&lt;Element&gt;;
  }
  
  interface Scopes {
      main: Elements;
      [other: string]: Elements;
  }
  </code></pre>
  
  <p>Then I have a function which takes some object of <code>Scopes</code> type and also parameter which defined which scopes should I merge into output object (this will be flattened without scopes). The function might look like:</p>
  
  <pre><code>function merge&lt;S extends Scopes&gt;(obj: S, selectors?: { [K in keyof S]?: boolean }): UNKNOWNTYPE {
     // this will be some logic to extract and merge scopes
  }
  </code></pre>
  
  <p>I have a problem with properly define output type for this function <code>UNKNOWNTYPE</code>. I want To dynamically flat and extract keys from input object so for folowing input data I will get this output:</p>
  
  <pre><code>const scopes: Scopes = {
    main : {
      a : {
         foo: "a"
      },
      b : {
         bar: "b"
      }
    },
    other : {
      b : {
         foo : "b"
      },
      c : {
         foo : "c"
      }
    }
  }
  
  const out = merge(scopes, { main : true, other : true });
  
  /*
  out is of this strcture
  
  {
    a : {
      foo : "a"
    },
    b : {
      foo : "b"
      bar : "b"
    },
    c : {
      foo : "c"
    }
  }
  */
  </code></pre>
  
  <p>I tried some approach and had tried such type but it does't resolve all merged scopes' keys:</p>
  
  <pre><code>interface UNKNOWNTYPE&lt;Scopes, Scope extends keyof Scopes = keyof Scopes&gt; {
      [K in keyof Scopes[Scope]: Element]
  }
  </code></pre>
  
  <p>Is there any way to dynamically flatten and intersect 2nd level object from nested object?</p>