| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
There was a problem hiding this comment.
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:
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 |
src/util.test.ts:567
test("Result.success creates a success result", (t) => {
Sorry, something went wrong.
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>;
Sorry, something went wrong.
Co-authored-by: Michael B. Gale <mbg@github.com>
|
(Discussed offline: the class-based approach lets us use a fluent API for functions like orElse, which is slightly more readable) |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
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:
we can now infer in the else branch that a.value must be a string.