The Hidden undefined Behind TypeScript Index Signatures

Update (2025-08): TypeScript 4.1 introduced the noUncheckedIndexedAccess compiler flag, which largely addresses the issue discussed below. When enabled, reading from an index signature automatically includes | undefined in the resulting type.

Index signatures come up fairly often in TypeScript. They describe data structures whose properties are accessed by bracket notation []—for example, TypeScript’s built-in Array<T> interface uses a numeric index signature:

interface Array<T> {
  ...

  [n: number]: T
}
typescript

In JavaScript, developers also tend to reach for plain objects over Map instances when building key-value maps. While Map is safer in several ways, it doesn’t serialize cleanly—JSON.stringify() doesn’t handle Map out of the box, which makes it awkward when passing data between frontend and backend. TypeScript calls this kind of dictionary object a Record, typically defined as type Record<K extends keyof any, T> = { [P in K]: T }.

However, if you’re counting on TypeScript for strict type safety—or hoping that code that compiles cleanly won’t blow up at runtime—you need to tread carefully around index signatures, or avoid them altogether. The type TypeScript infers when reading from an index signature is a lie:

const map: { [key: string]: number } = {
  foo: 1,
  bar: 2,
};

const value = map["baz"];
// value: number
typescript

Looking up a key that doesn’t exist evaluates to undefined at runtime, yet TypeScript’s inferred type (number) gives no hint of that possibility. In web applications where we constantly batch-process arrays and dictionary maps, it’s all too easy to forget about the undefined lurking behind a supposedly safe type—until it blows up at runtime.

Someone opened an issue on microsoft/TypeScript about this as early as 2017. Naturally, people immediately suggested the obvious fix: just tack | undefined onto the index signature’s value type. Unfortunately, that doesn’t actually solve the problem. When defining map above, we only want reads to reflect that a key might be missing. Adding | undefined to the value definition changes the semantics of the type itself: it no longer stops callers from explicitly writing undefined into the array or map, and it pollutes the return types of helpers like Object.values() with undefined.

There are two main reasons this remained unresolved for so long. For one, most people adopt TypeScript simply as a pragmatic layer on top of JavaScript—it already patches over plenty of JavaScript’s rough edges, and as a gradual type system it has nearly zero migration friction. For another, TypeScript has to maintain full compatibility with idiomatic JavaScript, which means JavaScript’s historical baggage inevitably constrains how safe the type system can be. Distinguishing get vs. set types on index signatures is just one piece of the puzzle; accounting for the whole language doesn’t easily yield an elegant solution.

I often think that a TypeScript untethered from JavaScript would make a much better language—just not a popular one. What we have today is the result of trade-offs. In the end, some people compromise, some rely on workarounds to preserve their inner peace, and some quietly wish for a world without JS.

All posts