the tswhy logo: a question mark in a box

tswhy‽

A community effort to enrich TypeScript diagnostics.

Editing TS1011:

All diagnostics and fixes are authored in markdown. Propose any changes by editing the markdown. Additional fixes can be added. A preview of the rendered diagnostic will update when changes are made.

Once all proposed changes are made, the Propose button will submit the information and confirm raising the PR.

TS1011

An element access expression should take an argument.

When accessing a property of an array or object with bracket notation, you will get this error if no property is supplied.

const a = [1, 2, 3]
const b = a[]

// or
const c = { d: 4 }
const d = c[]

Fix: Provide an index argument

To fix the error, provide an index or property:

const a = [1, 2, 3];
const b = a[1];

// or
const c = { d: 4 };
const d = c["d"];