For Either or Maybe or probably for any Foldable I cannot get working type inference. Every time I need to provide full generic types list.
fold<Error, Response, ReactNode>(
error => error.message,
users => <SomeComponent users={users} />
)(result)
TS is not inferencing the types. Generics are needed to be provided.
fold(
error => error.message,
users => <SomeComponent users={users} />
)(result)
To be clear result is fully typed variable but it just goes away in fold. error and users are members of uknown type
| Software | Version(s) |
| ---------- | ---------- |
| fp-ts | 2.0.5 |
| TypeScript | 3.6.2 |
This is due to TypeScript not being able to infer types from right to left. What you should do instead is to always use these functions together with pipe, e.g.
pipe(
result,
fold(e => e.message, u => u.name)
)
This is currently mentioned in the migration docs, but that might not be clear enough yet: https://gcanti.github.io/fp-ts/introduction/upgrade-to-v2.html
Am 12.09.2019 um 10:14 schrieb Maciej Sikora notifications@github.com:
🐛 Bug report
Current Behavior
For Either or Maybe or probably for any Foldable I cannot get working type inference. Every time I need to provide full generic types list.
fold
(
error => error.message,
users =>
)(result)
TS is not inferencing the types. Generics are needed to be provided.Expected behavior
fold( error => error.message, users => <SomeComponent users={users} /> )(result)To be clear result is fully typed variable but it just goes away in fold. error and users are members of uknown type
Software Version(s)
fp-ts 2.0.5
TypeScript 3.6.2
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub https://github.com/gcanti/fp-ts/issues/948?email_source=notifications&email_token=AAACUOYUZ5NC6OPQEM7XZVDQJH277A5CNFSM4IWAUZDKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HK5JV7Q, or mute the thread https://github.com/notifications/unsubscribe-auth/AAACUOYS7SD2AIK73PMTWFLQJH277ANCNFSM4IWAUZDA.
as @grossbart said (fold should always be used inside a pipe) plus, a clarification: Foldable is a type class that doesn't have anything to do with the fold function, despite the similar name.
The fact that fold is not encoded in any type class is also the reason why there's no "data-first" fold function available in the library, which is something I've myself wanted in a few occasions
Thanks guys, Everything clear now!
Most helpful comment
as @grossbart said (
foldshould always be used inside apipe) plus, a clarification:Foldableis a type class that doesn't have anything to do with thefoldfunction, despite the similar name.The fact that
foldis not encoded in any type class is also the reason why there's no "data-first"foldfunction available in the library, which is something I've myself wanted in a few occasions