Skip to content

Fix type guards throwing on null instead of returning false - #1910

Merged
Dirk Bäumer (dbaeumer) merged 2 commits into
microsoft:mainfrom
mdoin:fix/type-guards-null
Oct 22, 2026
Merged

Dirk Bäumer (dbaeumer) merged 2 commits into
microsoft:mainfrom
mdoin:fix/type-guards-null

Conversation

@mdoin

Copy link
Copy Markdown

Several type guards in types/src/main.ts check Is.defined(candidate) before reading properties of the candidate. Is.defined only rules out undefined, so for null they throw a TypeError instead of returning false:

Command.is(null);                                                     // TypeError: Cannot read properties of null (reading 'title')
CodeLens.is({ range, command: null });                                // same, through Command.is
Diagnostic.is({ range, message: 'm', relatedInformation: [null] });  // same, through DiagnosticRelatedInformation.is

Other guards in the same file, such as Position.is, Range.is and TextEdit.is, already return false for null, and the existing tests in typeguards.test.ts expect that. Some of the affected guards run on values received from the server, for example Command.is in asCodeActionResult and TextDocumentEdit.is when converting workspace edits.

The fix uses Is.objectLiteral(candidate) (value !== null && typeof value === 'object') in the 14 affected guards. That's the check Position.is, Range.is, TextEdit.is and the other object guards already use:

  • DiagnosticRelatedInformation, Diagnostic, Command
  • TextDocumentEdit, TextDocumentIdentifier, VersionedTextDocumentIdentifier, OptionalVersionedTextDocumentIdentifier, TextDocumentItem, TextDocument
  • CodeActionContext, CodeLens, FormattingOptions, DocumentLink, InlineValueContext

The behavior is unchanged for undefined, primitives and objects, including class instances. For example, TextDocument.is still accepts the class instance returned by TextDocument.create.

Tests: I added a table-driven suite to types/src/test/typeguards.test.ts. For each of these guards, a value built with its create function is accepted, and null and undefined are rejected. A separate test covers the nested null cases above.

I ran these checks on Node 22.17.0, the CI version, in the types package:

  • tsc -b ./types/tsconfig.json and eslint src are clean.
  • mocha: 56 passing, up from 41 on main.
  • With only the new tests applied to main, the 15 new tests fail with TypeError on property access of null.

I did not run the browser or VS Code integration tests. The change is limited to the types package.

Several type guards check `Is.defined(candidate)` before reading
properties of the candidate. `Is.defined` only rules out `undefined`,
so passing `null`, e.g. `Command.is(null)`, throws a TypeError instead
of returning false. This also happens through nested guards, e.g.
`CodeLens.is({ range, command: null })` or a diagnostic with a `null`
entry in `relatedInformation`.

Use `Is.objectLiteral(candidate)`, which the other type guards already
use, in the 14 affected guards.
@mdoin
Maitê Doin (mdoin) marked this pull request as ready for review October 22, 2026 13:29
Copilot AI balanced review requested due to automatic review settings October 22, 2026 13:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The narrowly scoped changes follow existing guard patterns and include regression coverage for every modified guard, with no blocking issues identified.

No open issues 🎉

What changed in this PR

This PR makes 14 shared type guards return false for null instead of throwing, following the object-checking pattern already used in the types package.

Changes:

  • Replace Is.defined with Is.objectLiteral before accessing candidate properties.
  • Add regression tests for valid values, null, and undefined across all modified guards.
  • Cover nested null values in code lenses, diagnostics, and code-action contexts.
File Description
types/​src/​test/​typeguards.test.ts Add table-driven and nested-null regression coverage.
types/​src/​main.ts Apply null-safe object checks to 14 type guards.

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 22, 2026 15:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused changes consistently reuse the existing guard pattern and cover every modified guard, with no blocking issues identified.

No open issues 🎉

Review effort: Balanced

@dbaeumer
Dirk Bäumer (dbaeumer) merged commit 26e11d4 into microsoft:main Oct 22, 2026
5 checks passed
@dbaeumer Dirk Bäumer (dbaeumer) added this to the 3.18.6 milestone Oct 22, 2026
@dbaeumer Dirk Bäumer (dbaeumer) added this to the 3.18.6 milestone Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants