Open
Conversation
Contributor
There was a problem hiding this comment.
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
Resultclass with two separate classes (SuccessandFailure) that form a discriminated union - Updated all call sites from
Result.success()andResult.failure()to usenew Success()andnew 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) => {
mbg
previously approved these changes
Feb 24, 2026
Member
mbg
left a comment
There was a problem hiding this comment.
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>;Co-authored-by: Michael B. Gale <mbg@github.com>
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.
Thanks @mbg for pointing out this drawback.
By defining
Result<T, E>as a sum typeSuccess<T> | Failure<E>, we can now infer that aResultis a failure if it is not a success. For example, if we have:we can now infer in the else branch that
a.valuemust be astring.