I have an array which is state entry that I populate in componentDidMount lifecycle hook, and it is quite simple:
state{ array:[{a:0,b:0},{a:1,b:1},{a:2,b:2}] } When I try to access it, however I am always getting an error:
Property 'a' does not exist on type 'never'.
I think I am having troubles with declaring an interface here, can't find a proper way to do that. Something like this would probably do:
interface IState{ array: object[] } Full version of component :
import * as React from "react"; import phrasalVerbs from "../data/phrasalVerbs"; interface IAnswerOptions { pVerb: string; meaning: string; } interface IState { pVerbs: object; progress: number; answerOptions: IAnswerOptions[]; } class PhrasalVerbs extends React.Component<{}, IState> { public state = { pVerbs: phrasalVerbs, progress: 0, answerOptions: [] }; public componentDidMount() { this.randomVariants(); } public randomVariants = () => { let randomVariants: any[] = []; const currentVerbs = this.state.pVerbs.data; const shuffledVerbs = currentVerbs .map(a => [Math.random(), a]) .sort((a: any, b: any): any => a[0] - b[0]) .map(a => a[1]); randomVariants = [shuffledVerbs[0], shuffledVerbs[1], shuffledVerbs[2]]; this.setState({ ...this.state, answerOptions: randomVariants }); }; public render() { const { pVerbs, progress } = this.state; return ( <div> <div>{pVerbs.data[progress].pVerb}</div> {/* <div> {this.state.answerOptions ? this.state.answerOptions[0].meaning : null} </div> */} </div> ); } } export default PhrasalVerbs;
c?