CodePark/ questions / Modules, .d.ts & tsconfig
START PRACTICINGPRACTICE
Frontend question · TypeScript › Modules, .d.ts & tsconfig

With noUncheckedIndexedAccess enabled, what is the type of first?

const list: string[] = getNames();
const first = list[0];
string | undefined

The answer is string | undefined. The option adds undefined to every read from an array or index signature, because the index might be out of range.

string is the tempting wrong answer. You get plain string only without the option, even when the array is empty.