Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Typescript赢得';t不允许我使用括号表示法访问对象的属性_Javascript_Reactjs_Typescript_Types - Fatal编程技术网

Javascript Typescript赢得';t不允许我使用括号表示法访问对象的属性

Javascript Typescript赢得';t不允许我使用括号表示法访问对象的属性,javascript,reactjs,typescript,types,Javascript,Reactjs,Typescript,Types,我有以下接口: interface IAnswersCount { nintendo: number; microsoft: number; sony: number; } interface IState { counter: number; questionId: number; question: string; answerOptions: AnswerType[]; answer: string; answersCount: IAnswersCou

我有以下接口:

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: '',
  });
无论我如何编写代码,我都无法消除以下错误:

元素隐式具有“any”类型,因为索引表达式不是“number”类型。


我不知道我做错了什么,非常感谢您的帮助。

您需要映射密钥,我编辑您的界面:

interface IAnswersCount{
[键:字符串]:数字,
任天堂:数字;
微软:数字;
索尼:数字;
}
//[键:字符串]:数字
//不是新属性,而是地图访问定义

您需要映射密钥,我编辑您的界面:

interface IAnswersCount{
[键:字符串]:数字,
任天堂:数字;
微软:数字;
索尼:数字;
}
//[键:字符串]:数字
//不是新属性,而是地图访问定义

您索引错误的状态属性:
state.answerOptions
(它是一个数组),而不是
state.answerscont

我想你的意思是:

const doSomething = (answer: string): void => {
  // other things happen here
  const value = state.answersCount[answer as keyof IAnswersCount]
}
或者,在没有断言的情况下:

const doSomething = (answer: keyof IAnswersCount): void => {
  // other things happen here
  const value = state.answersCount[answer]
}

您索引错误的状态属性:
state.answerOptions
(它是一个数组),而不是
state.answerscont

我想你的意思是:

const doSomething = (answer: string): void => {
  // other things happen here
  const value = state.answersCount[answer as keyof IAnswersCount]
}
或者,在没有断言的情况下:

const doSomething = (answer: keyof IAnswersCount): void => {
  // other things happen here
  const value = state.answersCount[answer]
}
有两个问题:

  • “答案”应使用较窄的类型
  • 我想您应该使用
    answerscont
    而不是
    answerOptions
    ,因为在您的示例中
    answerOptions
    AnswerType
    的数组,而不是具有字符串属性的对象
  • constdosomething=(答案:keyof IAnswersCount):void=>{
    //其他事情在这里发生
    常量值=状态。回答问题[回答];
    }
    
    如果您确实想使用
    应答选项
    ,请共享
    应答类型

    存在两个问题:

  • “答案”应使用较窄的类型
  • 我想您应该使用
    answerscont
    而不是
    answerOptions
    ,因为在您的示例中
    answerOptions
    AnswerType
    的数组,而不是具有字符串属性的对象
  • constdosomething=(答案:keyof IAnswersCount):void=>{
    //其他事情在这里发生
    常量值=状态。回答问题[回答];
    }
    

    如果您确实打算使用
    应答选项
    ,请共享
    应答类型

    我担心
    任何
    都会破坏使用Typescript的目的。您是对的,等待重写…看,我编辑您的界面,注意第一行我担心
    任何
    都会破坏使用Typescript的目的。您是对的,等待重写…看,我编辑你的界面,注意第一行
    状态的类型。answerOptions
    AnswerType[]
    ,所以它是一个数组。为什么要用字符串键访问它?作为IAnswersCount键的
    应答类型将是
    nintendo | microsoft | sony
    ,将其用作
    应答选项
    数组的键是没有意义的。如果你想根据答案访问选项,你必须重新考虑你的模型,可能需要使用映射/对象文字。你的意思是写
    state.answerscont[answer]
    而不是
    state.answerOptions[answer]
    ?@robertgr991哦,完全正确。当然,它不起作用,我试图进入错误的财产。实际上,我想针对
    回答
    。我的错。谢谢!顺便说一句,用useState设置一个完整的状态对象并不是最好的做法。您希望将每个属性分解为每个属性的useState调用,或者在此处使用reducer。在状态中使用对象是完全可以接受的,尤其是当对象中的属性被链接时。例如,计算属性
    c时需要属性
    a
    和属性
    b
    由于状态是异步的,有3个单独的状态来管理这些属性意味着无法保证在重新计算属性
    c
    时,
    a
    b
    会更新到最新。不需要使用减速机,但可能更容易理解。
    state.answerOptions
    的类型是
    AnswerType[]
    ,因此它是一个数组。为什么要用字符串键访问它?作为IAnswersCount键的
    应答类型将是
    nintendo | microsoft | sony
    ,将其用作
    应答选项
    数组的键是没有意义的。如果你想根据答案访问选项,你必须重新考虑你的模型,可能需要使用映射/对象文字。你的意思是写
    state.answerscont[answer]
    而不是
    state.answerOptions[answer]
    ?@robertgr991哦,完全正确。当然,它不起作用,我试图进入错误的财产。实际上,我想针对
    回答
    。我的错。谢谢!顺便说一句,用useState设置一个完整的状态对象并不是最好的做法。您希望将每个属性分解为每个属性的useState调用,或者在此处使用reducer。在状态中使用对象是完全可以接受的,尤其是当对象中的属性被链接时。例如,计算属性
    c时需要属性
    a
    和属性
    b
    由于状态是异步的,有3个单独的状态来管理这些属性意味着无法保证在重新计算属性
    c
    时,
    a
    b
    会更新到最新。不需要使用减速器,但可能更容易理解。