Report #4976
[bug\_fix] Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Config'. TS7053.
Narrow the key type from 'string' to 'keyof Config' using a type guard or by changing the function signature to accept K extends keyof Config, OR add an index signature to the Config interface \(e.g., \`\[key: string\]: string \| undefined;\`\) if dynamic access with arbitrary strings is intentional and safe. Root cause: With 'noImplicitAny' enabled \(implied by strict\), accessing an object property via a bracket notation variable of type 'string' \(rather than a union of known keys\) requires an index signature to guarantee type safety, otherwise the result would implicitly be 'any', subverting the type system.
Journey Context:
You're building a configuration loader that reads environment variables. You define \`interface Config \{ apiKey: string; timeout: number; \}\`. You receive a key name from a CLI argument: \`const key = process.argv\[2\];\` \(inferred as string\). You write \`const value = config\[key\];\`. TypeScript immediately highlights this with TS7053: 'Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Config'. You consider using \`\(config as any\)\[key\]\` to silence it, but that defeats the purpose of TypeScript. You search the error code and find the Handbook page on Index Signatures. You realize that because 'key' is just a string, TypeScript cannot guarantee that 'key' actually exists in Config \(e.g., it could be 'foobar'\). To maintain type safety, it requires you to either prove the key is valid \(narrowing\) or explicitly allow dynamic access. You decide the best approach is to narrow the type. You create a type guard: \`function isConfigKey\(k: string\): k is keyof Config \{ return k in config; \}\`. You then write: \`if \(isConfigKey\(key\)\) \{ console.log\(config\[key\]\); \}\`. Inside the if block, config\[key\] is now correctly typed as string \| number. The error is gone, and your code is safe against invalid keys.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T20:23:47.447154+00:00— report_created — created