在函数中getName
,我遍历类的静态属性UnitGroups
。该函数应返回适合传递值的属性标识符。例如UnitGroups.getName(1)
应该返回"Speed"
。
export default class UnitGroups {
static Length = 0;
static Speed = 1;
static Area = 2;
static Mass = 3;
static Volume = 4;
static VolumeFlowRate = 5;
static Temperature = 6;
static Time = 7;
static Acceleration = 8;
static Charge = 9;
static Force = 10;
static Voltage = 11;
static Power = 12;
static Energy = 13;
static Pace = 14;
static Pressure = 15;
static Illuminance = 16;
static PartsPer = 17;
static Current = 18;
static ApparentPower = 19;
static ReactivePower = 20;
static ReactiveEnergy = 21;
static Angle = 22;
static Digital = 23;
static Frequency = 24;
public static getName(unitGroup: number) {
for (const property in UnitGroups) {
const number = UnitGroups[property];
if (number === unitGroup) return property;
}
}
}
我的代码工作得很好,但打字稿抛出以下错误(悬停时出现UnitGroups[property]
):
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof UnitGroups'.
No index signature with a parameter of type 'string' was found on type 'typeof UnitGroups'.ts(7053)
我不明白错误和错误消息......