At Figma, we believe in continuously investing in product quality and developer productivity by looking at recurring types of bugs and addressing them in a systematic way. A few months ago, our engineering team completed a large effort that achieves both: turning on the strictNullChecks compiler flag for our front-end TypeScript codebase.

在 Figma,我们相信通过关注反复出现的错误类型并以系统的方式解决这些问题,来不断投资于产品质量和开发人员的生产力。几个月前,我们的工程团队完成了一项大型工作,实现了这两点:为我们的前端 TypeScript 代码库打开了strictNullChecks 编译器标志。

Code written with strict null checks operates under different type-checking rules and has a markedly different flavor than code written without. Converting our codebase to use it was a large undertaking that we think serves as an interesting case study in code migration.


In this blog post, we’d like to share how we performed this migration incrementally without slowing down concurrent product development.


What are strict null checks?


In many popular typed programming languages (e.g. TypeScript, Java, etc), most or all variables are allowed to have the value null, indicating the absence of a more specific value.

在许多流行的类型化编程语言中(如TypeScript、Java等),大多数或所有的变量都允许有null ,表示没有一个更具体的值。

// strictNullChecks: off

interface Vector { x: number, y: number }

var v: Vector = { x: 1, y: 2 } // ✅ This is allowed
v = null // ✅ This is also allowed

function length(v: Vector) {
 // We may need to check for nullity when we're not sure.
 if (v) {
 return Math.sqrt(v.x * v.x + v.y * v.y)
 } else {
 // Return some default value?
 // What does it even mean to call length(null)?

Most of us are used to this. However, this is not a fundamental property of programming languages. It was an intentional design choice by Tony Hoare back in 1965, who would later call it his billion dollar mistake.

我们大多数人都习惯于此。然而,这并不是编程语言的一个基本属性。这是托尼-霍尔(Tony Hoare)在1965年有意做出的设计选择,他后来称这是他的十亿美元的错误

And there are actually languages that do it diffe...


首页 - Wiki
Copyright © 2011-2023 iteam. Current version is 2.109.0. UTC+08:00, 2023-03-23 08:55
浙ICP备14020137号-1 $访客地图$