Skip to content
webtype.orgwebtype.orgtsconfig

    ↑↓ move · ⏎ open · esc close

    tsconfig

    noUncheckedIndexedAccess

    An index signature is a guess

    Reading a key that was never written gives `undefined` at runtime. This is the flag that says so in the type.

    Since
    TypeScript 4.1
    strict
    Not in strict
    In your tsconfig
    "noUncheckedIndexedAccess": true
    Compiled with
    "strict": true

    The same snippet both times. Only the option changed.

    With it off

    "noUncheckedIndexedAccess": false
    const rows: Record<string, string> = {}
    
    export const first: string = rows.a

    Compiles clean

    With it on

    "noUncheckedIndexedAccess": true
    const rows: Record<string, string> = {}
    
    export const first: string = rows.a

    Emits TS2322

    Why the compiler bothers

    The most surprising thing about `strict` is that it does not include this. `Record<string, string>` claims every one of the infinitely many string keys holds a string, which is never true of any real object, and the same hole is in every array access — `items[5]` on a three-element array is `undefined` and typed as the element. It is not under `strict` because turning it on rewrites every loop body and every lookup in a codebase, and that was judged too much to impose by default. It is still the right setting.

    Takeaway

    Turn it on early in a project or never. Retrofitting it is the expensive one.

    Where to go next

    22 options