#Type 'false' cannot be used as an index type?

5 messages · Page 1 of 1 (latest)

fallow basalt
#

I have an object where one of the keys is false.
I want to access it via myObj[false] which is valid JS, but typescript doesn't like it and gives me the error

Type 'false' cannot be used as an index type

boreal muralBOT
#
Hailwood#7433

Preview:```ts
const items = {
false: "false",
yes: "y",
no: "n",
}

console.log(items[false])```

knotty jackal
#

{ false: "false" } is equivalent to { "false": "false" }; object keys are only strings, numbers, or symbols

#

if that works at runtime, then it works because of a conversion, which TS generally disallows even though "it works at runtime", as such code is usually not intentional

fallow basalt
#

Ah right! Thanks