'Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{}'

I have an object declared and properties assigned like this:

const decoded = JSON.parse(nk.binaryToString(message.data));

const matchStateChanges = {
  humans: {},
  ball: {},
}

const { name } = decoded // string e.g. "Player1"
matchStateChanges.humans[name] = state.humans[name] // object type e.g. {pos: {12,13}}

But the compiler breaks with:

$ npx tsc

match_control.ts:145:7 - error TS7053: Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{}'. 145 matchStateChanges.humans[name] = state.humans[name] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

What am I doing wrong?



Solution 1:[1]

This worked:

// Declare a custom interface to type the object
interface HumanObject {
  [index: string]: object
}

..

const humans: HumanObject = {}

// we'll build this up when we loop messages so we only transmit what's changed
const matchStateChanges = {
  humans,
  ball: {},
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martyn