new to deno/typescript, the main idea is variable kindx will be from the command line and some info in the types object will be shown, but always get this error:
error: TS7053 [ERROR]: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ A: string; B: string; }'.
No index signature with a parameter of type 'string' was found on type '{ A: string; B: string; }'.
let obj = types[kindx]
here is the code:
const types = {
A: 'Apple',
B: 'Banana'
}
let kindx = 'B' // will be from Deno.args[0]
//const kindx = 'B' // this works
let obj = types[kindx]
console.log(obj)
A
let
variable can be change on runtime so TypeScript implicitly give him the typeany
. To fix that, you need to define the type ofkindx
: