Я использую следующие циклы while и switch-case. Я пытаюсь обработать все различные значения перечисления в перечислении constants.Sides
.
// EDIT Константы импортируются в начало файла.
import * as constants from '../constants'
// ... other code not included
let crossedSide: constants.Sides | undefined;
let loopGuard = 0;
while ((crossedSide = this.hasCrossedBorder()) && loopGuard < 2) {
loopGuard += 1;
switch (crossedSide) {
case constants.Sides.TOP: { // THE ERROR HAPPENS HERE
this.velocity.y = -this.velocity.y;
break;
}
case constants.Sides.RIGHT: {
this.velocity.x = -this.velocity.x;
break;
}
case constants.Sides.BOTTOM: {
this.velocity.y = -this.velocity.y;
break;
}
case constants.Sides.LEFT: {
this.velocity.x = -this.velocity.x;
break;
}
default:
break;
}
}
Перечисление определено в constants/index.ts
следующим образом:
export enum Sides {
TOP,
RIGHT,
BOTTOM,
LEFT,
}
Тем не менее, возникает ошибка в параметре constants.Sides.TOP
case-case:
Тип 'Sides.TOP' не сопоставим с типом ' Стороны. ПРАВО | Sides.BOTTOM | Sides.LEFT.