title: Typescript: accessing an array element does not account for the possibility of undefined return values
body: <p>The following passes the Typescript type checker (v2.9.1), but throws a <code>TypeError</code> at run time.</p>
  
  <pre><code>interface Item { id: string }
  const list: Item[] = [{ id: 'a' }, { id: 'b' }];
  const item = list[3]; // type: Item
  const itemId = item.id; // type: string
  </code></pre>
  
  <p>Given that accessing an element in a typed array could always return <code>undefined</code>, shouldn't item be <code>item: Item | undefined</code>, which would force you to do a null check?</p>
  
  <p>Even more surprisingly to me, the following also type checks:</p>
  
  <pre><code>const item2: Item | undefined = list[3];
  const item2Id = item2.id;
  </code></pre>
  
  <p>though casting the returned value does successfully fail the type check:</p>
  
  <pre><code>const item3 = list[3] as Item | undefined;
  const item3Id = item3.id; // [ts] Object is possibly 'undefined'.
  </code></pre>
  
  <p>Creating an explicitly typed accessor function also catches the undefined case, but adds unnecessary overhead:</p>
  
  <pre><code>const getItem1 = (index: number, items: Item[]): Item | undefined =&gt; items[index];
  const item3 = getItem1(3, list);
  const item3Id = item3 &amp;&amp; item3.id;
  </code></pre>
  
  <p>Is this a known limitation of typescript?  Are there recommended patterns or libraries for handling this case?</p>