Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve types of Result.{isOk, isErr, value} #44

Closed
wants to merge 1 commit into from

Conversation

ysulyma
Copy link

@ysulyma ysulyma commented Nov 8, 2023

Improve types of Result.{isOk, isErr, value}

This improves type narrowing as shown below:

const x = {} as unknown as Result<string, number>;

const y = x.value(); // string | number

if (x.isOk()) {
  const y = x.value(); // string
} else {
  const y = x.value(); // number
}

if (x.isErr()) {
  const y = x.value(); // number
} else {
  const y = x.value(); // string
}
  • I have added or updated unit tests for this change. (N/A)
  • I have updated the changelog with info about the changes in this PR

@ysulyma
Copy link
Author

ysulyma commented Nov 8, 2023

hmm actually this breaks some existing code. I think the library should do this, but it might require more significant changes to the types...

@rametta
Copy link
Owner

rametta commented Nov 8, 2023

Thanks for the PR @ysulyma. I've been meaning to improve the type narrowing for awhile. Ideally we would use Higher Kinded Types (HKT's) for this. I have another library which I have used that concept before and it works flawlessly (https://github.com/rametta/srd) feel free to take a look, that concept can be applied here too

@rametta rametta closed this Dec 18, 2023
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.

2 participants