Hacker News new | ask | show | jobs
by throwaway284534 1709 days ago
You’re right that this is really easy to mess up, especially when defining an array index e.g.

    const todos: string[] = [“walk dog”];
    todos[123].toUpperCase() // error!
IMO non constant (as defined by TypeScript) arrays should’ve been automatically assigned a union type with `undefined`, which can also be a fix for Records too:

    type Tips = Record<string, TipObject | undefined>
1 comments

You’re looking for the noUncheckedIndexAccess compiler option: https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAc...