-
-
Notifications
You must be signed in to change notification settings - Fork 503
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
Revise 'left' and 'right' typings for typesafety #543
Comments
@sledorze thanks, looks good to me. Maybe we could use export declare const right: <L = never, A = never>(a: A) => Either<L, A> There are other modules that can be improved
|
I used 'typeof a' because of an inference fatigue habit (it often has fewer defect). The interesting case is someone who wants to take advantage and use that signature: right<number>('what?') But you made me realise some issues: right: <L = never, A = any>(a: A) => Either<L, A>
right<number>('what?') // Type is Either<number, any> (WRONG) right: <L = never, A = never>(a: A) => Either<L, A>
right<number>('what?') // Argument of type '"what?"' is not assignable to parameter of type 'never'. (WRONG) right: <L = never, A = never>(a: A) => Either<L, typeof a>
right<number>('what?') // Argument of type '"what?"' is not assignable to parameter of type 'never'. (WRONG) right: <L = never, A = any>(a: A) => Either<L, typeof a>
right<number>('what?') // Type: Either<number, any> (WRONG) So we must prevent any partial Type Param Bindings: right: <L = never, A = 'reason is you cannot partially bind Type Params to `right`'>(a: A) => Either<L, typeof a>
right<number>('what?') // Argument of type '"what?"' is not assignable to parameter of type '"reason is you cannot partially bind Type Params to `right`"' That's still really acceptable (and a great improvement over the current status). |
@gcanti Agree |
@gcanti actually, I've taken the time to read the PR (yesterday was a busy day). The reason being that what this PR implements is a call site resolution. Otherwise, inference will still generates Wrong types. Also it requiers to write extra code where it shouldn't do anything in the first place. To summarise: Still errors by default and more verbose. I've tested the new sign just on those two 'left' and 'right' constructors and rebuild our code base; no problem and I've also been able to remove all explicit typings on the few case I've tried. @gcanti Maybe we should start with Either module and see how it works? What do you think? |
@gcanti I've made a branch with a POC implementation. |
No, my concern has more to do with not knowing how the new feature will play with default type arguments, i.e // x1: Either<never, number>
const x1 = right(1) // ok
// x2: Either<string, number>
const x2 = right<string, number>(1) // ok
// x3: Either<string, number> or Either<string, never> ?
const x3 = right<string, infer>(1) // ok? That is, will So I'd wait for that PR to be merged in order to play with it by installing |
@gcanti you meant: // x3: Either<string, number> or Either<string, 'reason is you cannot partially bind Type Params to `right`'> ?
const x3 = right<string, infer>(1) // ok? Note: we should maybe find a better message, it is nice as part of an error message but does not really make sense otherwise. |
This has been solved with version 1.19.1 |
Today's typing for 'left' and 'right' are unsafe due to the way typescript does it's inference and causing us some trouble.
Example:
Thanks to the way union behave with never
With these typings inference works as expected
I know this would be a breaking change,
so here's a backward compatible non breaking solution:
Do you see any issue with it? (Being typesafe is priceless)
The text was updated successfully, but these errors were encountered: