Issue
this is part of my code:
const myObj: object = {}
const propname = 'propname'
myObj[propname] = 'string'
but I got error:
ERROR in path/to/file.ts(4,1)
TS7053: Element implicitly has an 'any' type because expression of type '"propname"' can't be used to index type '{}'.
Property 'propname' does not exist on type '{}'.
What is wrong here, and how can I fix it?
Solution
You have to define what kind of index type the object has. In your case it is a string
based index.
const myObj: {[index: string]:any} = {}
Answered By - Murat Karagöz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.