Skip to content

QuizNot attempted

Spot the bug: types that hide bugs

Which of these typing choices let real bugs compile?
interface TableColumn { key: string; label: string }
const [resourceData, setResourceData] = useState<TableColumn[]>([]);
setResourceData(await rawRes.json()); // usage records, not columns
// Table.tsx
interface TableData { [key: string]: any }
<td>{row[column.key]}</td>

Select all that apply.

1–4 to choose, Enter to submit
Show answer & explanation

Correct answers:

  • resourceData holds usage records but is typed as column definitions; res.json() returns any, so it is accepted
  • The any index signature lets a cell be an object (a runtime crash) or a mixed type in the sort, with no error
  • column.key is a plain string, so a typo in a column key compiles and renders an empty column
  • any switches checking off wherever it flows.
  • Type the record (`type Usage = { id: string
  • cost: number
  • date: string
  • ... }), make the table generic (Column<T> = { key: keyof T & string
  • label: string }, Table<T>), and validate JSON at the boundary (a schema library or a type guard) so any` stops there.
  • Not required for dynamic data: Record<string, unknown> compiles fine
  • you narrow a value before rendering it, which is exactly the check any skips.