我不知道返回枚举成员的函数的类型是什么。这是我到目前为止的代码:
interface Letter {
character: string,
color: Function
}
enum Color {
Green,
Yellow,
Grey
}
const letter_1: Letter = {
character: playerInput[0],
color: () => {
if (letter_1.character === playerInput[0])
return Color.Green;
else {
for (let i = 0; i < playerInput.length; i ++) {
if (letter_1.character === playerInput[i])
return Color.Yellow;
}
}
return Color.Grey;
}
};
目前在Letter
接口中,我将color()
的类型设置为Function
,但我认为它一定有另一种类型,我只是不知道。
您要查找的类型是() => Color
,即返回Color
枚举值的无参数函数:
interface Letter {
character: string,
color: () => Color
}
enum Color {
Green,
Yellow,
Grey
}
const letter: Letter = {
character: '',
color: () => Color.Green
};