I have been struggling with a question on how to correctly use index in TS for some time now. For example...
setup.battle.enemies.forEach(enemy => {
const _thisMove = enemy.movements.random();
enemy.curMovement = {
name : _thisMove.name,
damage : _thisMove.elemental.damage(),
element : _thisMove.elemental.element,
setMove() {
const _resistances = variables().player.resistances;
let _enemyDamage = Math.round(this.damage - _resistances[this.element]);
}
}
})
In this scenario, _resistances[this.element] is causing an error: Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{ physical: number; fire: number; ice: number; energy: number; water: number; light: number; darkness: number; }'.ts(7053)
The resistances object in the enemies class is defined as...
this.resistances = {
physical : 2,
fire : 0,
ice : 0,
energy : 0,
water : 0,
light : -2,
darkness : 0
};
So my question is, how can I resolve this issue and make it work properly?
The element property is part of the enemies skills array.
this.movements = [
{
name : "Knife attack",
elemental : {
element : "physical",
damage : () => random(2, 4),
getElem : function() { return this.element }
}
}
]