How to provide a type definition when pushing elements into an array using TypeScript?
P粉745412116
2023-07-24 21:47:12
<p>I need to avoid using 'any' as a type definition when pushing elements into an array. I tried providing the type but I got an error. Here is sample code: </p>
<pre class="brush:php;toolbar:false;">interface answerProps {
state: string;
answer: string;
}
const results: Array<string> = [];
!isEmpty(answers) &&
answers.map((item: any) => results.push(`${item.state} : ${item.answer}`));</pre>
<p>Based on the above code, I want to avoid using a while loop to map array elements. </p>
When mapping arrays, just use the answerProps interface.