HiveBrain v1.2.0
Get Started
← Back to all entries
debugtypescriptCritical

Typescript error: TS7053 Element implicitly has an 'any' type

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
implicitlyanyerrorhasts7053typeelementtypescript

Problem

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} = {}

Code Snippets

const myObj: {[index: string]:any} = {}

Context

Stack Overflow Q#56833469, score: 365

Revisions (0)

No revisions yet.