CodeRoadMap
首页路线题库资源库文章社区
CodeRoadMap

程序员的学习成长路线图。内容免费开放,登录后可同步路线图与课时进度。

由 程序员柏风 独立维护 · 关于作者

学习

  • 路线图
  • 课程
  • 面试题
  • 文章
  • 我

更多

  • 资源
  • 社区
  • 关于作者
  • 内容说明

© 2026 CodeRoadMap

津ICP备2026012044号-1·津公网安备12011002026155号·coderoadmap@126.com
TypeScript 进阶/课时/类与 OOP
RawBlame
课时目录12 节

TypeScript 进阶

  1. 01TS 进阶地图
  2. 02原始类型深入
  3. 03集合类型
  4. 04断言与收窄
  5. 05内置工具类型
  6. 06高级类型
  7. 07类与 OOP
  8. 08函数类型进阶
  9. 09装饰器
  10. 10命名空间
  11. 11工具链
  12. 12综合练习

类与 OOP

classes、abstract、inheritance、access modifiers。

35 分钟

类与 OOP

TypeScript 的类在 JavaScript class 语法上增加了 访问修饰符、抽象类、implements、readonly 等类型能力。本课系统讲解 OOP 建模,为 nestjs-essentials 的 Controller/Service 分层与 nextjs-essentials 中少量 class-based 工具(如 Error 子类)打基础,并衔接 typescript-essentials 的接口知识。

学习目标

  • 使用 public/private/protected/readonly 控制成员可见性
  • 用 abstract class 定义可扩展基类
  • 理解 implements 与 extends 的分工

基本类与修饰符

class TrackService {
  readonly apiBase: string;

  constructor(apiBase: string) {
    this.apiBase = apiBase;
  }

  public async list(): Promise<Track[]> {
    return this.fetchAll();
  }

  private async fetchAll(): Promise<Track[]> {
    const res = await fetch(`${this.apiBase}/tracks`);
    return res.json();
  }

  protected log(message: string): void {
    console.log(`[TrackService] ${message}`);
  }
}

interface Track {
  slug: string;
  title: string;
}

继承与 super

class CachedTrackService extends TrackService {
  private cache = new Map<string, Track>();

  override async list(): Promise<Track[]> {
    if (this.cache.size > 0) {
      return [...this.cache.values()];
    }
    const tracks = await super.list();
    tracks.forEach((t) => this.cache.set(t.slug, t));
    return tracks;
  }
}

抽象类

abstract class Repository<T extends { id: number }> {
  abstract findById(id: number): Promise<T | null>;
  abstract save(entity: T): Promise<T>;

  async exists(id: number): Promise<boolean> {
    const found = await this.findById(id);
    return found !== null;
  }
}

class LessonRepository extends Repository<Lesson> {
  async findById(id: number): Promise<Lesson | null> {
    return null;
  }
  async save(entity: Lesson): Promise<Lesson> {
    return entity;
  }
}

interface Lesson {
  id: number;
  title: string;
}

implements 接口

interface Logger {
  info(message: string): void;
  error(message: string, err?: unknown): void;
}

class ConsoleLogger implements Logger {
  info(message: string): void {
    console.log(message);
  }
  error(message: string, err?: unknown): void {
    console.error(message, err);
  }
}

参数属性(Parameter Properties)

class TracksController {
  constructor(
    private readonly service: TrackService,
    protected readonly logger: Logger,
  ) {}

  getAll(): Promise<Track[]> {
    this.logger.info("listing tracks");
    return this.service.list();
  }
}

练习

  • 用 abstract Repository 实现 LessonRepository
  • 对比 class implements 与 type 结构的异同
  • 阅读 NestJS Controller 源码,标出 public/private 用法

小结

类 = JS 运行时结构 + TS 访问控制;abstract 定义模板,implements 约束契约。 下一课函数类型进阶。

加载本节练习…
← 上一课高级类型下一课 →函数类型进阶