typescripttsconfig.json

TypeScript bracket notation and dot notation differences with "noImplicitAny": true/false


With "noImplicitAny": true in tsconfig.json:

const a = {}
console.log(a['b']) // 👈 Property b does not exist on type {}.
console.log(a.b) // 👈 Property b does not exist on type {}.

With "noImplicitAny": false in tsconfig.json:

const a = {}
console.log(a['b']) // 👈 no error
console.log(a.b) // 👈 Property b does not exist on type {}.

Why there's no error for the second console.log(a['b']) while the second console.log(a.b) throws?


Solution

  • See microsoft/TypeScript#43431 for an authoritative answer to this question.

    Using bracket notation is intentionally allowed so that you can perform otherwise prohibited indexes. So if foo.bar is disallowed, you can work around it with foo["bar"]. But when you do that and the property isn't known to exist, the resulting property type is implicitly any. As such, you get an error if and only if --noImplicitAny is enabled.

    You'll notice that the error for a["b"] is different from the error for a.b when --noImplicitAny is enabled. They both mention that b doesn't exist on type {}, but for a["b"] you also get: Element implicitly has an 'any' type because expression of type '"b"' can't be used to index type '{}'.