title: TypeScript function return type based on input parameter
body: <p>I have a few different interfaces and objects that each have a <code>type</code> property. Let's say these are objects stored in a NoSQL db. How can I create a generic <code>getItem</code> function with a deterministic return type based on its input parameter <code>type</code>? </p>
  
  <pre><code>interface Circle {
      type: "circle";
      radius: number;
  }
  
  interface Square {
      type: "square";
      length: number;
  }
  
  const shapes: (Circle | Square)[] = [
      { type: "circle", radius: 1 },
      { type: "circle", radius: 2 },
      { type: "square", length: 10 }];
  
  function getItems(type: "circle" | "square") {
      return shapes.filter(s =&gt; s.type == type);
      // Think of this as items coming from a database
      // I'd like the return type of this function to be
      // deterministic based on the `type` value provided as a parameter. 
  }
  
  const circles = getItems("circle");
  for (const circle of circles) {
      console.log(circle.radius);
                         ^^^^^^
  }
  </code></pre>
  
  <blockquote>
    <p>Property 'radius' does not exist on type 'Circle | Square'.</p>
  </blockquote>