-
Notifications
You must be signed in to change notification settings - Fork 22
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
Fixed types of Either.fromTry #59
base: master
Are you sure you want to change the base?
Conversation
Hey. It looks like the typing is good now because you can provide the types by generic types application. Why did you decide to change them? |
I saw the issue, but, let's just change the documentation. Could I ask you to do the separated PR? |
@JSMonk |
No, your PR is great, I just want to split it in two: one with the changes to the readme, and another one with all the logic |
I can. What's the benefit of it though? |
Seems like that in the case of throwing an error inside the function provided to the |
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.
not sure about those (donʼt have much time to check it), not sure if extend
ing unknown
is sensible or not really… (i.e. to go with L extends unknown = unknown
but probably not many types naturally extend unknown
s…!)
either/index.ts
Outdated
@@ -193,11 +193,11 @@ class EitherConstructor<L, R, T extends EitherType = EitherType> | |||
return EitherConstructor.right(v); | |||
} | |||
|
|||
static fromPromise<L, R>(promise: Promise<R>): Promise<Either<L, R>> { | |||
static fromPromise<R>(promise: Promise<R>): Promise<Either<unknown, R>> { |
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.
you could probably also go for something like this:
static fromPromise<R>(promise: Promise<R>): Promise<Either<unknown, R>> { | |
static fromPromise<R, L = unknown>(promise: Promise<R>): Promise<Either<L, R>> { |
not sure of all the consequences, though!
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.
Fixed
either/index.ts
Outdated
return promise.then(EitherConstructor.right).catch(EitherConstructor.left); | ||
} | ||
|
||
static fromTry<L, R>(fn: () => R): Either<L, R> { | ||
static fromTry<R>(fn: () => R): Either<unknown, R> { |
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.
once again,
static fromTry<R>(fn: () => R): Either<unknown, R> { | |
static fromTry<R, L = unknown>(fn: () => R): Either<L, R> { |
is an option…? 🤔
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.
This is exactly what I propose.
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.
Fixed
c0bd847
to
93542bf
Compare
Docs in another PR: #66 |
@@ -193,11 +194,11 @@ class EitherConstructor<L, R, T extends EitherType = EitherType> | |||
return EitherConstructor.right(v); | |||
} | |||
|
|||
static fromPromise<L, R>(promise: Promise<R>): Promise<Either<L, R>> { | |||
static fromPromise<R, L = unknown>(promise: Promise<R>): Promise<Either<L, R>> { |
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.
Could we save the previous order of params by adding the unknown
default value to the R
param too?
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.
That would be counter intuitive since whenever working with promises most of the the time you care about the result which is now first generic argument. From the other side this is "breaking" change.
Thoughts?
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.
It's a breaking change. I will put it into the new major version
Addresses comment: #53 (comment)