title: How to use the typeof property from bracket notation
body: <p>I'm learning TS and I'm creating a sort function that sort my array of objects using a params as key using bracket notation.
  The code above works well, but if I want to extend in the future, i should add every conditions. I want to validate by the type of the value of the key that I want to use.</p>
  <p>I try things like the code above and it works to validate but TS keep throwing errors that property/methods inside the IF are not valid because it takes all existing types from every key of my interface and not only the ones that really can go through the condition</p>
  <pre><code>if(typeof arr[prop] === &quot;string&quot;)
  if(arr[prop] instanceof String)
  if(arr[prop].constructor.name === &quot;String&quot;)
  </code></pre>
  <p>This is the code</p>
  <pre><code>  interface Person{
    name: string,
    age: number,
    isAlive: boolean}
  
  const myArr = [
      {name: 'Tomás',
      age: 24,
      isAlive: true},
      {name: 'Kate',
      age: 12,
      isAlive: true},
      {name: 'Brad',
      age: 54,
      isAlive: false},
      {name: 'Angelica',
      age: 64,
      isAlive: true},
  ]
  
  function myFn(
      arr: Person[],
      orderBy: keyof Person): Person[] {
          if(orderBy === 'name'){
              return arr.sort((a,b) =&gt; a[orderBy].localeCompare(b[orderBy]));
          }
          else if (orderBy === &quot;age&quot;) {
            return arr.sort((a, b) =&gt; a[orderBy] - b[orderBy]);
          } 
          else if (orderBy === &quot;isAlive&quot;) {
            return arr.sort(value =&gt; value[orderBy] ? -1 : 1)
          }
          else {
              return [];
          }
    }
  
    const byAlive = myFn(myArr, &quot;isAlive&quot;);
    console.log(byAlive)
    const byAge = myFn(myArr, &quot;age&quot;);
    console.log(byName)
    const byName = myFn(myArr, &quot;name&quot;);
    console.log(byName)
  
  </code></pre>
  <p>Thanks in advance and sorry for my English, I'm not native :)
  If I'm not clear with something or you can't understand me, please let me know so I try to write it in a better way.</p>