41 lines
783 B
TypeScript
41 lines
783 B
TypeScript
type StudentId = number | string;
|
|
|
|
interface Course {
|
|
title: string;
|
|
finished: boolean;
|
|
}
|
|
|
|
interface Student {
|
|
id: StudentId;
|
|
name: string;
|
|
age?: number;
|
|
courses: Course[];
|
|
}
|
|
|
|
function pickFirst<T>(list: T[]): T {
|
|
return list[0];
|
|
}
|
|
|
|
function formatStudent(student: Student): string {
|
|
// 返回一段摘要文字
|
|
return `学号:${student.id}
|
|
姓名:${student.name}
|
|
年龄:${student.age?.toFixed()}`;
|
|
}
|
|
|
|
const student: Student = {
|
|
id: "stu-1",
|
|
name: "林晨",
|
|
courses: [
|
|
{ title: "基本类型", finished: true },
|
|
{ title: "接口", finished: false },
|
|
],
|
|
};
|
|
|
|
console.log(formatStudent(student), pickFirst(student.courses));
|
|
|
|
// 任务:
|
|
// 1. 实现 formatStudent
|
|
// 2. 用 pickFirst 取第一门课程
|
|
// 3. 输出摘要和第一门课程
|