Repository navigation
Fix type guards throwing on null instead of returning false - #1910
Merged
Dirk Bäumer (dbaeumer) merged 2 commits intoOct 22, 2026
Merged
Conversation
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.
There was a problem hiding this comment.
🟢 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.definedwithIs.objectLiteralbefore accessing candidate properties. - Add regression tests for valid values,
null, andundefinedacross all modified guards. - Cover nested
nullvalues 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
Dirk Bäumer (dbaeumer)
approved these changes
Oct 22, 2026
Alex Ross (alexr00)
approved these changes
Oct 22, 2026
Federico Brancasi (federicobrancasi)
approved these changes
Oct 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Several type guards in
types/src/main.tscheckIs.defined(candidate)before reading properties of the candidate.Is.definedonly rules outundefined, so fornullthey throw aTypeErrorinstead of returningfalse:Other guards in the same file, such as
Position.is,Range.isandTextEdit.is, already returnfalsefornull, and the existing tests intypeguards.test.tsexpect that. Some of the affected guards run on values received from the server, for exampleCommand.isinasCodeActionResultandTextDocumentEdit.iswhen converting workspace edits.The fix uses
Is.objectLiteral(candidate)(value !== null && typeof value === 'object') in the 14 affected guards. That's the checkPosition.is,Range.is,TextEdit.isand the other object guards already use:DiagnosticRelatedInformation,Diagnostic,CommandTextDocumentEdit,TextDocumentIdentifier,VersionedTextDocumentIdentifier,OptionalVersionedTextDocumentIdentifier,TextDocumentItem,TextDocumentCodeActionContext,CodeLens,FormattingOptions,DocumentLink,InlineValueContextThe behavior is unchanged for
undefined, primitives and objects, including class instances. For example,TextDocument.isstill accepts the class instance returned byTextDocument.create.Tests: I added a table-driven suite to
types/src/test/typeguards.test.ts. For each of these guards, a value built with itscreatefunction is accepted, andnullandundefinedare rejected. A separate test covers the nestednullcases above.I ran these checks on Node 22.17.0, the CI version, in the
typespackage:tsc -b ./types/tsconfig.jsonandeslint srcare clean.mocha: 56 passing, up from 41 onmain.main, the 15 new tests fail withTypeErroron property access ofnull.I did not run the browser or VS Code integration tests. The change is limited to the
typespackage.