专栏目标
| 目标 | 说明 |
|---|---|
| 打破“八股文”认知 | 不再死记硬背,理解设计思想 |
| 结合编程范式 | OOP vs FP,让你知其所以然 |
| 贴近真实项目 | CLI、React、Node.js 实战 |
| 提升架构思维 | 从“写代码”到“设计类型” |
专栏目录
第 1 章:从八股文说起 —— 那些年我们背过的“标准答案” /InterfaceVsType/Part01.md
markdown
- “`interface` 可以合并,`type` 不行”
- “`type` 适合联合类型,`interface` 不行”
- “能用 `interface` 就用 `interface`”
- 问题:这些“规则”在真实项目中失效了第 2 章:本质区别 —— 名义类型 vs 结构类型 /InterfaceVsType/Part02.md
markdown
- TypeScript 是结构类型系统
- `interface` 是“开放契约”,支持声明合并
- `type` 是“封闭别名”,强调不可变
- 举例:第三方库扩展 vs 内部 DTO 定义第 3 章:OOP 之魂 —— 何时用 interface /InterfaceVsType/Part03.md
markdown
- 类的契约:`implements`
- 继承体系:`extends`
- 插件系统:声明合并(如 Redux 中间件)
- 适用场景:大型类库、企业级 OOP 架构第 4 章:FP 之道 —— 何时用 type /InterfaceVsType/Part04.md
markdown
- 代数数据类型(ADT):`Result<T> = Success | Failure`
- 组合优于继承:`User = Name & Email & Timestamp`
- 高阶类型:`type Pick<T, K> = ...`
- 适用场景:React props、状态管理、CLI 配置第 5 章:真实项目中的选择 —— 我们是如何决定的 /InterfaceVsType/Part05.md
markdown
- CLI 工具:用 `type CliCommand`
- React 组件:用 `type Props`
- Node.js 服务:用 `interface UserService`
- 领域模型:混合使用,`interface User` + `type Events`第 6 章:现代 TypeScript 趋势 —— type 的崛起 /InterfaceVsType/Part06.md
markdown
- Zod、io-ts 推动函数式类型编程
- `satisfies` 操作符让 `type` 更安全
- 构建时类型检查取代运行时 `instanceof`
- 未来:`type` 成为主流第 7 章:反模式 —— 你可能正在犯的错误 /InterfaceVsType/Part07.md
markdown
- 盲目 `Omit<yargs.CommandModule, 'command'>`
- 为了“可扩展”用 `interface`,结果没人扩展
- 在 DTO 中用 `interface` 导致意外合并第 8 章:决策树 —— 一张图教你选 type 还是 interface /InterfaceVsType/Part08.md
markdown
┌─────────────┐
│ 你在定义一个类的契约吗? │
└─────────────┘
│
┌───────────────┴───────────────┐
是 否
│ │
┌───────────┴───────────┐ ┌─────────────┴─────────────┐
│ 要被多个类实现吗? │ │ 是数据组合或配置吗? │
└───────────────────────┘ └──────────────────────────┘
│ │
┌───────┴───────┐ ┌───────┴───────┐
是 否 是 否
│ │ │ │
┌──────▼──────┐ ┌──────▼──────┐ ┌───────▼────────┐ ┌────▼────────┐
│ 用 interface │ │ 用 type │ │ 用 type │ │ 用 type │
└─────────────┘ └─────────────┘ └────────────────┘ └─────────────┘第 9 章:TypeScript 类型设计哲学 /InterfaceVsType/Part09.md
markdown
- 类型是文档,也是约束
- 类型应反映业务意图,而非技术细节
- “可维护性” > “可扩展性”(除非真需要)
- 类型设计是一门艺术第 10 章:结语 —— 超越八股文 /InterfaceVsType/Part010.md
markdown
- 面试官问“`type` 和 `interface` 区别”时,
不要背八股,讲你的项目经验。
- 展示你如何**权衡、决策、演进**。
- 这才是高级工程师的价值。