TypeScript 中的逆变、协变、双变与不变
在 TypeScript 的类型系统中,逆变(Contravariance)、协变(Covariance)、双变(Bivariance) 和 不变(Invariance) 是描述类型兼容性和子类型关系的重要概念。这些术语来源于类型理论,主要用于处理函数类型、泛型和接口的赋值规则。理解这些概念不仅能帮助我们编写更安全的代码,还能让我们更好地设计类型系统。本文将逐一讲解这四种变型,辅以代码示例,让你轻松掌握它们的含义与应用。
在 TypeScript 的类型系统中,逆变(Contravariance)、协变(Covariance)、双变(Bivariance) 和 不变(Invariance) 是描述类型兼容性和子类型关系的重要概念。这些术语来源于类型理论,主要用于处理函数类型、泛型和接口的赋值规则。理解这些概念不仅能帮助我们编写更安全的代码,还能让我们更好地设计类型系统。本文将逐一讲解这四种变型,辅以代码示例,让你轻松掌握它们的含义与应用。
Node.js is built on Google’s V8 engine, and V8’s garbage collection mechanism is one of its core performance features. In this article, we’ll explore the three primary garbage collection algorithms used in V8: Mark-Sweep, Mark-Compact, and Scavenge, along with their working principles and application scenarios.
Before diving into garbage collection algorithms, we need to understand V8’s memory partition model. V8 divides memory into two main regions:
Vite 的速度优势主要来源于其基于原生 ESM(ES Modules)的开发模式和高效的构建过程。
<script type="module">,可以动态加载 ESM 模块,Vite 的开发服务器将源码直接以 ESM 格式提供给浏览器。通过改写[].__proto__上的方法,实现对Array原生方法的拦截。
源码位置为 /core/instance/observer/array.js
1 | // cache the original Array.prototype |