我有以下接口:
interface IAnswersCount {
nintendo: number;
microsoft: number;
sony: number;
}
interface IState {
counter: number;
questionId: number;
question: string;
answerOptions: AnswerType[];
answer: string;
answersCount: IAnswersCount;
result: string;
}
以及功能组件内部的状态,如下所示:
const [state, setState] = React.useState<IState>({
counter: 0,
questionId: 1,
question: '',
answerOptions: [],
answer: '',
answersCount: {
nintendo: 0,
microsoft: 0,
sony: 0,
},
result: '',
});
在代码中的某处,我试图动态访问属性的嵌套属性之一answersCount。我这样做的方式是:
const doSomething = (answer: string): void => {
// other things happen here
const value = state.answerOptions[answer as keyof IAnswersCount]
}
无论我如何编写代码,我都会收到以下我无法摆脱的错误:
Element implicitly has an 'any' type because index expression is not of type 'number'.
我无法弄清楚我做错了什么,非常感谢任何帮助。