title: Does TypeScript have difficulty using Generics for indexing?
body: <p>Expanding on an example from Effective Typescript I can't tell if I'm running into a limitation of the type checker or my understanding.</p>
  <p>Here's the example in its raw form from page 33</p>
  <pre><code>interface Point {
    x: number
    y: number
  }
  
  function sortBy&lt;K extends keyof T, T&gt;(vals: T[], key: K): T[] {
    // ...
  }
  
  const pts: Point[] = [
    { x: 1, y: 1 },
    { x: 2, y: 0 },
  ]
  
  console.log(sortBy(pts, &quot;x&quot;))
  </code></pre>
  <p>Here's my attempt to replace //... with something useful</p>
  <pre><code>interface Point {
    x: number
    y: number
  }
  
  function sortBy&lt;K extends keyof T, T&gt;(vals: T[], key: K): T[] {
    // my solution
    return vals.sort((a, b) =&gt; {
      return b[key] - a[key] // Error: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.ts(2362)
    })
    // end my solution
  }
  
  const pts: Point[] = [
    { x: 1, y: 1 },
    { x: 2, y: 0 },
  ]
  
  console.log(sortBy(pts, &quot;x&quot;))
  </code></pre>
  <p>But if I replace with</p>
  <pre><code>return (b[key] as any) - (a[key] as any)
  </code></pre>
  <p>it works</p>
  <p>What I can't figure out is why does the type checker think a[key] or b[key] could be anything other than a number, since a and b must be type Point, and key must be keyof Point.</p>
  <p>If I write this without generics it works fine, which makes me think something hinky is going on here.</p>
  <pre><code>function sortBy(vals: Point[], key: &quot;x&quot; | &quot;y&quot;): Point[] {
    return vals.sort((a, b) =&gt; {
      return b[key] - a[key]
    })
  }
  </code></pre>