Text of code.visualstudio.com › blogs › 2019 › 05 › 23 › strict-null as Styx crawled it on Nov 18, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Strict null checking Visual Studio Code

Join VS Code at Microsoft Ignite or catch the sessions online. A year of VS Code, wrapped. Explore the highlights. On this page Safety permits speed Moving fast is fun. It's fun to ship new features, make users happy, and improve our codebases. But, at the same time, it's not fun to ship a buggy product. No one likes getting issues or being woken up for an incident at three in the morning. Although moving fast and shipping stable code are often presented as being incompatible, that shouldn't be the case.

Many times the same factors that make code fragile and buggy are also what slow down development. After all, how can we move fast if we're always worried about breaking things? In this post, I'd like to share a major engineering effort that the VS Code team recently completed: enabling TypeScript's strict null checking in our codebase. We believe this work will allow us to both move faster and to ship a more stable product.

Enabling strict null checking was motivated by understanding bugs not as isolated events but as symptoms of larger hazards in our source code. Using strict null checking as a case study, I'm going to discuss what motivated our work, how we came up with an incremental approach to addressing the problem, and how we went about implementing the fix. This same general approach to identifying and reducing hazards can be applied to any software project.

An example To illustrate the problem VS Code was facing before enabling strict null checking, let's consider a simple TypeScript library. Don't worry if you're new to TypeScript; the specifics are not important. This make-believe example is only meant to illustrate the class of problem we were hitting in the VS Code codebase, as well as mentioning some traditional responses to such problems. Our example library consists of a single getStatus function that fetches a given user's status from the backend of a hypothetical website: export interface User { readonly id : string ; } /** * Get the status of a user */ export async function getStatus ( user : User ): Promise < string > { const id = user . id ; const result = await fetch ( `/api/v0/ ${ id } /status` ); const json = await result . json (); return json . status ; } Looks reasonable.

Ship it! But after deploying our new code, we see a spike of crashes. From the call stack, it looks like the crashes are happening in our getStatus function. Uh oh! Tracing back a little farther, it seems one of our fellow engineers is calling getStatus(undefined) in a misguided attempt to get the status of the current user. This causes an exception when the code tries to access: undefined.id . Simple mistake. And now that we know the cause, let's fix it!

So we update the calling code, update getStatus to handle undefined , and also add a helpful warning in our doc comment: /** * Get the status of a user * * Don't call this with undefined or null! */ export async function getStatus ( user : User ): Promise < string > { if (! user ) { return '' ; } const id = user . id ; const result = await fetch ( `/api/v0/ ${ id } /status` ); const json = await result . json (); return json . status ; } And because we are totally real engineers, we also write a test: it ( 'should return empty status for undefined user' , async () => { assert . equals ( getStatus ( undefined ), '' ); }); Great!

No more crashes. And we're even back at 100% test coverage too! Our code must be perfect now. A few days go by and then: BOOM! Someone notices something strange in our logs, a huge number requests to /api/v0/undefined/status . That's an odd user name... So we investigate again, fix the code again, add more tests. Maybe also send a passive-aggressive email to whoever was calling getStatus({ id: undefined }) . /** * Get the status of a user * * !!! * WARNING: Don't call this with undefined or null, or with a user without an id * !!! */ export async function getStatus ( user : User ): Promise < string > { if (! user ) { return '' ; } const id = user . id ; if ( typeof id !== 'string' ) { return '' ; } const result = await fetch ( `/api/v0/ ${ id } /status` ); const json = await result . json (); return json . status ; } Perfect.

But, just to make sure, let's require that all changes that introduce a call to getStatus be approved by a senior engineer. That should permanently put a stop to these pesky bugs... And maybe this time we go a few more days before the next crash. Maybe a few months even. But, unless our code is never changed again, there will be one. If not in this specific function, then somewhere else in our codebase. To make matters worse, every change now requires: defensively checking for undefined , changing tests or adding new tests, and getting team sign-off.

What gives? We're all doing our part and yet there are still bugs! There's got to be a better way. Identifying the hazard While the bugs in the example above may seem obvious, we were hitting the same type of problems while developing VS Code. Every iteration, we would fix bugs related to an unexpected undefined . And we would add tests. And we would vow to be better engineers. Those are all traditional responses and yet in the next iteration, it would happen all over again.

Not only was this causing some users to have a poor experience with VS Code, these bugs and our responses to them were also slowing us down while working on new features or changing existing source code. We realized that we needed to start understanding our bugs in a new way, not as isolated events but as symptoms/signals of larger problems. Our responses to these bugs and our frustration at not being able to move quickly were also symptoms.

When we started discussing the root causes of these symptoms, we found a few common ones: Failure to catch simple programming mistakes, such as accessing properties on null or undefined . Under-specified interfaces. Which parameters can be undefined or null , and which functions may return undefined or null ? Often the implementer of the function was working under a different set of assumptions than the callers. Type oddities. undefined vs null . undefined vs false . undefined vs empty string.

Feeling that we could not trust the code or safely refactor it. Identifying the root causes was a good first step, but we wanted to go even deeper. What were the hazards in all these cases that allowed a well meaning engineer to introduce the bug in the first place? And we quickly identified a glaring hazard common to all these issues: the lack of strict null checking in the VS Code codebase. To understand strict null checking, you have to remember that TypeScript's aim is to add typing to JavaScript.

A consequence of TypeScript's JavaScript legacy is that, by default, TypeScript allows undefined and null to be used for any value: // Without strict null checking, all of these calls are valid getStatus ( undefined ); // Ok getStatus ( null ); // Ok getStatus ({ id: undefined }); // Ok While this flexibility makes it simpler to migrate from JavaScript to TypeScript, the example library for our hypothetical website showed that it is also a hazard.

This hazard was also central to the four root causes we had identified working on VS Code (plus many others). Thankfully though, TypeScript provides an option called strict null checking that causes undefined and null to be treated as distinct types. When using strict null checking, any type that may be nullable must be annotated as such: // With "strictNullCheck": true, all of these produce compile errors getStatus ( undefined ); // Error getStatus ( null ); // Error getStatus ({ id: undefined }); // Error Fixing isolated lines of code or adding tests was a reactive solution that only fixed those specific bugs.

Enabling strict null checking is a proactive solution that not only would fix the bugs we were seeing reported every month, but would also prevent these entire classes of bugs from happening in the future. No more forgetting to check if an optional property has a val

The rest of this page is on the live site.