Record<string, T> lies about index-lookup optionality
Type-checking a TS refactor that added an in-flight cache map
Default TypeScript declares Record<string, T> as if every string key maps to a value of type T. The compiler types cache[key] as T, not T | undefined, even though at runtime an unset key returns undefined. So an if (cache[key]) ... guard makes TS warn "this condition will always return true" because per the types the value is non-optional. There are three honest fixes: (1) declare the type as Record<string, T | undefined> so index lookups correctly produce a unionable value; (2) use the in operator (key in cache) which doesnt lie about presence; or (3) enable noUncheckedIndexedAccess in tsconfig, which makes ALL index lookups produce T | undefined globally. Most codebases havent flipped the global flag, so the per-field fix is the path of least resistance — annotate the map type with | undefined explicitly, the rest of the code (guards, ?? fallbacks) becomes accurate again.
When introducing any "lookup might miss" map in a TS codebase that doesnt have noUncheckedIndexedAccess enabled, declare it as Record<string, T | undefined>. The guard pattern youre about to write (if (map[key])) will only type-check if the value type is already optional. Discover this at write time, not when the type-checker fails after a long edit pass.