title: Infer return value of nested selectors
body: <p>I want to infer the values of a nested selector. Meaning that the second selector uses the result of the first selector as an argument to select a sub piece of this value.</p>
  <pre><code>function store&lt;
      T,
      Selector extends (a: T) =&gt; unknown,
      ReturnValue extends ReturnType&lt;Selector&gt;,
      NestedSelector extends (a: ReturnValue) =&gt; unknown,
      ReturnValueNestedSelector extends ReturnType&lt;NestedSelector&gt;
  &gt;(state: T, selector: Selector) {
      const get = () =&gt; selector(state) as ReturnValue;
      const getNested = (nestedSelector: NestedSelector) =&gt;
          nestedSelector(selector(state) as ReturnValue) as ReturnValueNestedSelector;
  
      return {
          get,
          getNested,
      };
  }
  
  const initialData = {
      foo: {
          bar: {
              id: 1,
          },
      },
  };
  
  const myStore = store(initialData, (a) =&gt; a.foo.bar);
  
  const a = myStore.get(); // { id: number }
  const result = myStore.getNested((state) =&gt; {
      return state.id; // number
  }); // unknown
  
  The first selector is working okayish (I still need to cast the return value) but the getNested function returns unknown. Is there a way to solve this in TS?
  </code></pre>
  <p><a href="https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABAZynATgUwDwCgCQAKgDQEDKmANptBopgB5SZgAmyiAFAIYBcihAJSIAvAD5E4ANZg4AdzCl8AJUxQQ6MADVulEJnpMW7RKvWbCATwAOOCtVroxSgHKZUmVvZpp0h5mwcPPxmGtq6%20sLikmAy8ooEoZo6ephuHl5UPnSMASZJYFa22OnMmQ6%20YrhinKjczPwkKFmO-N6OwgDeBBAIqIgA5mqiXFES3YiTU5NY5kjILb61UPWYwtwcBSn6ANy40wC%20ez19UINqpZ4jnGDuZe2%20-Jfl2ehjiN34%20LNhiLcZDwwnCGUE4gnWmzUYW2aTunkB6H2UwOuAIP00HwI%20BBSmxFzhrCUR1wKNwvTA-RgYBgsF0ABF6twRp9gHA4PxPvgAEbcdAcrH4GCsfgARlxByJpGJZNOiAAtpYyL4DCIUMrOFSaTB6YziFxuO9uAA6VlwI08t7Hcn9JmqhVKjCYI0gsE7RAAendH0QQv4YBAcq5mD8KOtZywyBAlDOdsVyud%20IynGWq3en3R8xWzCNQrdnr%20AaDiIOgjzXuksgUuCAA" rel="nofollow noreferrer">Playground</a></p>