mobily / ts-belt

🔧 Fast, modern, and practical utility library for FP in TypeScript.
https://mobily.github.io/ts-belt
MIT License
1.11k stars 31 forks source link

TS2345 when using Result with pipe #59

Closed ChambreNoire closed 1 year ago

ChambreNoire commented 1 year ago

Hello,

I'm getting a TS error when using Result and pipe and have no idea why!

const processEvent = (
    event: MessageEvent<unknown>,
    handle: (event: ServerEvent) => void,
    error: (error: string) => void
) => {
    pipe(
        R.fromNullable(event, 'Incoming sse/ws message is null!'),
        R.map((event) => event.data), // <--- ERROR HERE
        R.flatMap((data: unknown) => {
            try {
                if (G.isString(data)) return R.Ok(JSON.parse(data));
            } catch (_error) {
                /* ignored */
            }
            return R.Error('Invalid incoming sse/ws message format!');
        }),
        R.flatMap((json: string) => {
            try {
                return R.Ok(ServerEventSchema.parse(json));
            } catch (_error) {
                return R.Error('Invalid incoming sse/ws message schema!');
            }
        }),
        R.tapError(error),
        R.tap(handle)
    );
};

The message I get is the following:

TS2345: Argument of type 'Result<unknown, unknown>' is not assignable to parameter of type '(arg: Result<MessageEvent<unknown>, "Incoming sse/ws message is null!">) => Result<unknown, "Invalid incoming sse/ws message format!">'.   Type 'Ok<unknown>' is not assignable to type '(arg: Result<MessageEvent<unknown>, "Incoming sse/ws message is null!">) => Result<unknown, "Invalid incoming sse/ws message format!">'.     Type 'Ok<unknown>' provides no match for the signature '(arg: Result<MessageEvent<unknown>, "Incoming sse/ws message is null!">): Result<unknown, "Invalid incoming sse/ws message format!">'.

Any ideas?

Thanks!

mobily commented 1 year ago

@ChambreNoire what ts-belt version do you use?

ChambreNoire commented 1 year ago

@mobily I'm using v3.13.1 with Typescript v4.9.4

mobily commented 1 year ago

v3.13.1

could you try with v4.0.0-rc.1? (you can install that version with the @next tag)

ChambreNoire commented 1 year ago

@mobily yup that did the trick! thanks!

mobily commented 1 year ago

@ChambreNoire awesome! 🎉 glad I could help :)