You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Understand that arrays are objects, so their keys are strings, not numbers. number as an index signature is a purely TypeScript construct designed to help catch bugs.
Prefer Array, tuple, ArrayLike, or Iterable types to using number in an index signature yourself.
constxs=[1,2,3];constx0=xs[0];// OKconstx1=xs['1'];// stringified numeric constants are also OKconstinputEl=document.getElementsByTagName('input')[0];constxN=xs[inputEl.value];// ~~~~~~~~~~~~~ Index expression is not of type 'number'.
functioncheckedAccess<T>(xs: ArrayLike<T>,i: number): T{if(i>=0&&i<xs.length){returnxs[i];}thrownewError(`Attempt to access ${i} which is past end of array.`)}