Files
front-end-example/06-typescript/08-final-mini-app/answer.ts
charlie f3bdaa4e88 feat: add TypeScript lessons and learning panel
- Introduced a new script to check TypeScript lesson files for errors.
- Created a main TypeScript file to render lessons and their details.
- Added lesson definitions with starter and answer codes.
- Implemented a user interface for navigating and running lessons.
- Styled the application with CSS for a better user experience.
- Updated README to reflect the new TypeScript section and usage instructions.
2026-03-19 10:06:11 +08:00

37 lines
719 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 {
const courseCount = student.courses.length;
return `${student.name} 当前有 ${courseCount} 门课程,编号是 ${student.id}`;
}
const student: Student = {
id: "stu-1",
name: "林晨",
courses: [
{ title: "基本类型", finished: true },
{ title: "接口", finished: false },
],
};
const firstCourse = pickFirst(student.courses);
console.log(formatStudent(student));
console.log(firstCourse);