//string let studentName: String = "xiaoming"; //number let studentAge: number = 18; //Boolean let isChecked: Boolean = true; //Date let now: Date = new Date(); //定义接口 interface StudentInt { name: string; age: number; isChecked: boolean; works?: Array; } //定义函数接口 interface CheckCountFunction { (studentList: Array): number; } //Array let studentList1: Array<{ name: string; age: number; isChecked: boolean }> = [ { name: "xiaoming", age: 18, isChecked: true }, { name: "xiaowang'", age: 18, isChecked: true }, ]; let studentnew: StudentInt = { name: "xiaoming", age: 18, isChecked: false, works: ["yuwen"], }; studentList1.push(studentnew); /**接口:实现提示词检索 * @param progress {number} 任务进度 * @param complete {boolean} 任务是否完成 * @param errorMsg {string} 错误信息 */ interface AgentTask { progress: number; //任务进度 complete: boolean; //任务是否完成 errorMsg: string; //错误信息 } let task1: AgentTask = { progress: 1, complete: true, errorMsg: "youwenti" }; /** * 统计人数 没有接口表示 * @param studentList{Arrayitem.isChecked ? 1:0) // let count = 0; // checkList.forEach(check=>{ // if(check==1) // count ++ // }) // let checkList = studentList.map((item): 1 | 0 => (item.isChecked ? 1 : 0)); // let count = 0; // checkList.forEach((checked) => { // if (checked == 1) count++; // }); // 使用reduce方法直接累加isChecked为true的学生数量 function checkCount1(studentList: Array): number { return studentList.reduce((count, student) => { return count + (student.isChecked ? 1 : 0); }, 0); } /** * 统计人数 用函数接口表示 * @param studentList{Array ): number => { let checkList = studentList.map((item): 1 | 0 => (item.isChecked ? 1 : 0)); let count = 0; checkList.forEach((checked) => { if (checked === 1) count++; }); return count; }; let count = checkCount(studentList1); console.log(count);