Fp-ts: Type inference is not working for fold

Created on 12 Sep 2019  ·  3Comments  ·  Source: gcanti/fp-ts

🐛 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, Response, ReactNode>(
                error => error.message,
                users => <SomeComponent users={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 |

Most helpful comment

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

All 3 comments

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FruitieX picture FruitieX  ·  3Comments

mohsensaremi picture mohsensaremi  ·  3Comments

bioball picture bioball  ·  4Comments

bobaaaaa picture bobaaaaa  ·  4Comments

mmkal picture mmkal  ·  3Comments