TypeScriptTips

Make bugs impossible.
One TypeScript tip at a time.

I like the tricks :) – Kasia

Unchecked indexed access

Increase type-safety with `noUncheckedIndexedAccess`, which is not included in `strict`.

// tsconfig.json
// "strict": true

declare const xs: number[]

const index = xs[xs.length - 1]
//    ^? ⛔️ number
const at = xs.at(-1)
//    ^? ✅ number | undefined
// tsconfig.json
// "noUncheckedIndexedAccess": true

declare const xs: number[]

const index = xs[xs.length - 1]
//    ^? ✅ number | undefined
const at = xs.at(-1)
//    ^? ✅ number | undefined