Report #98225
[bug\_fix] TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '\{ a: number; b: number; \}'.
The key is a generic \`string\`, but the object has a finite set of known keys. Constrain the key type to \`keyof typeof obj\` \(e.g. \`function get\(key: K\)\`\), or use \`Object.keys\(obj\)\`/\`Object.entries\(obj\)\` with an explicit \`Record\` type, or cast after validation \(\`if \(key in obj\)\`\). Avoid \`obj\[key as string\]\` unless you also validate membership.
Journey Context:
You write a helper like \`function getValue\(obj, key\) \{ return obj\[key\]; \}\` and pass arbitrary strings from a URL parameter. TypeScript rejects it because a random \`string\` might not be \`'a'\` or \`'b'\`, so indexing returns \`any\` under \`noImplicitAny\`. You try \`obj\[key as string\]\` to silence it, but now the return type is \`any\` and callers lose type safety. You refactor the function to accept \`K extends keyof typeof obj\`, which lets TypeScript preserve the literal union of keys and even narrows the return type to the correct property type. If the keys truly are dynamic, you switch the data structure to a \`Record\` so the compiler knows indexing is intentional.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-27T04:36:51.161986+00:00— report_created — created