datetype.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //string
  2. let studentName: String = "xiaoming";
  3. //number
  4. let studentAge: number = 18;
  5. //Boolean
  6. let isChecked: Boolean = true;
  7. //Date
  8. let now: Date = new Date();
  9. //定义接口
  10. interface StudentInt {
  11. name: string;
  12. age: number;
  13. isChecked: boolean;
  14. works?: Array<string>;
  15. }
  16. //定义函数接口
  17. interface CheckCountFunction {
  18. (studentList: Array<StudentInt>): number;
  19. }
  20. //Array
  21. let studentList1: Array<{ name: string; age: number; isChecked: boolean }> = [
  22. { name: "xiaoming", age: 18, isChecked: true },
  23. { name: "xiaowang'", age: 18, isChecked: true },
  24. ];
  25. let studentnew: StudentInt = {
  26. name: "xiaoming",
  27. age: 18,
  28. isChecked: false,
  29. works: ["yuwen"],
  30. };
  31. studentList1.push(studentnew);
  32. /**接口:实现提示词检索
  33. * @param progress {number} 任务进度
  34. * @param complete {boolean} 任务是否完成
  35. * @param errorMsg {string} 错误信息
  36. */
  37. interface AgentTask {
  38. progress: number; //任务进度
  39. complete: boolean; //任务是否完成
  40. errorMsg: string; //错误信息
  41. }
  42. let task1: AgentTask = { progress: 1, complete: true, errorMsg: "youwenti" };
  43. /**
  44. * 统计人数 没有接口表示
  45. * @param studentList{Array<StudentInt} 学生列表
  46. * @returns
  47. */
  48. // let checkList = studentList.map(item=>item.isChecked ? 1:0)
  49. // let count = 0;
  50. // checkList.forEach(check=>{
  51. // if(check==1)
  52. // count ++
  53. // })
  54. // let checkList = studentList.map((item): 1 | 0 => (item.isChecked ? 1 : 0));
  55. // let count = 0;
  56. // checkList.forEach((checked) => {
  57. // if (checked == 1) count++;
  58. // });
  59. // 使用reduce方法直接累加isChecked为true的学生数量
  60. function checkCount1(studentList: Array<StudentInt>): number {
  61. return studentList.reduce((count, student) => {
  62. return count + (student.isChecked ? 1 : 0);
  63. }, 0);
  64. }
  65. /**
  66. * 统计人数 用函数接口表示
  67. * @param studentList{Array<StudentInt} 学生列表
  68. * @returns
  69. */
  70. const checkCount: CheckCountFunction = (
  71. studentList: Array<StudentInt>
  72. ): number => {
  73. let checkList = studentList.map((item): 1 | 0 => (item.isChecked ? 1 : 0));
  74. let count = 0;
  75. checkList.forEach((checked) => {
  76. if (checked === 1) count++;
  77. });
  78. return count;
  79. };
  80. let count = checkCount(studentList1);
  81. console.log(count);