Skip to content

strict null checks in TypeScript #4941

@Veetaha

Description

@Veetaha

While studying your source code to finish with rust-lang/rust-analyzer#3053 I've stumbled with these lines of code.

const downloadUrl: string = build.assets.find(asset => {
return asset.name === vsixName;
}).browser_download_url;
if (!downloadUrl) {
throw new Error(`Failed to find VSIX: ${vsixName} in build: ${build.name}`);
}

From my perspective, it is apparent that if the release doesn't contain the needed .vsix file (which is a developer mistake that should never happen) you will get TypeError: Cannot read property 'browser_download_url' of undefined, instead of undefined written to downloadUrl. Thus, that true branch of the if statement is unreachable now.

Proof

image

In order to avoid such mistakes I strongly recommend you to set "strictNullChecks": true in tsconfig.json. The migration process for it is pretty straightforward, you just slap non-null assertions (i.e. postfix ! operator) everywhere the compiler sees a bug and later fix these places.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions