GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Improve type inference of `Result<T, E>` by henrymercer · Pull Request #3506 · github/codeql-action

Conversation

@henrymercer

@henrymercer henrymercer commented Feb 24, 2026

Copy link
Copy Markdown
Contributor

Thanks @mbg for pointing out this drawback.

By defining Result<T, E> as a sum type Success<T> | Failure<E>, we can now infer that a Result is a failure if it is not a success. For example, if we have:

const a = new Success(2) as Result<number, string>;
if (a.isSuccess()) {
  logger.info(`Success with value ${a.value}`);
} else {
  logger.info(`Failure with error ${a.value}`);
}

we can now infer in the else branch that a.value must be a string.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@henrymercer
Improve type inference of Result<T, E>
160d27b
@henrymercer
henrymercer requested a review from a team as a code owner February 24, 2026 17:43
Copilot AI review requested due to automatic review settings February 24, 2026 17:43
@github-actions github-actions Bot added the size/S Should be easy to review label Feb 24, 2026
Copilot started reviewing on behalf of henrymercer February 24, 2026 17:44
View session
Copilot AI reviewed Feb 24, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Pull request overview

This pull request refactors the Result<T, E> type from a single class with static factory methods to a discriminated union of Success<T> and Failure<E> classes. This improves TypeScript's type inference, allowing the compiler to correctly narrow types in conditional branches when checking isSuccess() or isFailure().

Changes:

  • Replaced the single Result class with two separate classes (Success and Failure) that form a discriminated union
  • Updated all call sites from Result.success() and Result.failure() to use new Success() and new Failure() constructors
  • Updated tests to reflect the new API

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/util.ts Refactored Result<T, E> from a single class to a sum type with Success<T> and Failure<E> classes implementing a common ResultLike interface
src/util.test.ts Updated tests to use new Success() and new Failure() instead of static factory methods
src/init-action.ts Updated imports and usages to construct Success and Failure instances directly
lib/init-action.js Generated JavaScript code mirroring the TypeScript changes
Comments suppressed due to low confidence (1)

src/util.test.ts:567

  • The test description still references "Result.success" but the implementation now uses "new Success()". Consider updating the test description to match the new API, for example: "Success creates a success result".
test("Result.success creates a success result", (t) => {

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/util.test.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

@henrymercer
Update names in tests
e51b6a9
mbg
mbg previously approved these changes Feb 24, 2026
View reviewed changes

@mbg mbg left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM with one minor comment. Also, was there a particular advantage you found to the class-based approach over something like:

type Success<T> = { isSuccess: true, value: T };
type Failure<E> = { isSuccess: false, value: E };
type Result<T, E> = Success<T> | Failure<E>;

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/util.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

@henrymercer @mbg
Update JSDoc
2a607fe
Co-authored-by: Michael B. Gale <[email protected]>
@henrymercer
henrymercer dismissed mbg’s stale review via 2a607fe February 24, 2026 19:28
mbg
mbg approved these changes Feb 24, 2026
View reviewed changes
@henrymercer
henrymercer merged commit 16adc4e into main Feb 24, 2026
244 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@henrymercer
henrymercer deleted the henrymercer/result-better-inference branch February 24, 2026 20:05
@henrymercer

henrymercer commented Feb 24, 2026

Copy link
Copy Markdown
Contributor Author

(Discussed offline: the class-based approach lets us use a fluent API for functions like orElse, which is slightly more readable)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This was referenced Feb 27, 2026
Merge main into releases/v4 #3523
Merged
Merge releases/v4 into releases/v3 #3533
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

Copilot code review Copilot
Copilot review effort
Applies to this pull request for everyone.Learn more about Copilot code review.
Copilot left review comments
@mbg mbg mbg approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

size/S Should be easy to review

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@henrymercer @mbg
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.