我需要避免在将元素推入数组时使用 'any' 作为类型定义。我尝试提供类型,但是出现了错误。以下是示例代码:
interface answerProps {
state: string;
answer: string;
}
const results: Array<string> = [];
!isEmpty(answers) &&
answers.map((item: any) => results.push(`${item.state} : ${item.answer}`));
根据上述代码,我想避免使用while循环来映射数组元素。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
在映射数组时,只需使用answerProps接口。
interface answerProps { state: string; answer: string; } const results: Array<string> = []; !isEmpty(answers) && answers.map((item: answerProps) => results.push(`${item.state} : ${item.answer}`));