Skip to content
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

Fix the AsyncResult.andThen handling of known-Ok transformations #189

Merged
merged 4 commits into from
Feb 7, 2025

Conversation

jstasiak
Copy link
Collaborator

@jstasiak jstasiak commented Feb 5, 2025

Without these overloads

goodResult.andThen((value) => Promise.resolve(Ok(value * 2)));

was of type

AsyncResult<number, unknown>

which would cause downstream problems.

Granted, this could be an edge case and the map method could be better for this (Ok -> Ok transformation so only the value inside changes) but still, I don't think we should be generating unknown here, hence the patch.

Resolves: #176

Without these overloads

    goodResult.andThen((value) => Promise.resolve(Ok(value * 2)));

was of type

    AsyncResult<uknown, string | number>

which would cause downstream problems.

Granted, this could be an edge case and the map method could be better
for this (Ok -> Ok transformation so only the value inside changes) but
still, I don't think we should be generating unknown here, hence the patch.

Resolves: #176
@jstasiak jstasiak changed the title Fix the AsyncResult.andThen type to handle known-Ok transformation Fix the AsyncResult.andThen handling of known-Ok transformations Feb 5, 2025
Copy link

@raguiar9080 raguiar9080 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jstasiak jstasiak merged commit 9259f83 into master Feb 7, 2025
2 checks passed
@jstasiak jstasiak deleted the fix-asyncresult-andthen branch February 7, 2025 11:50
jstasiak added a commit that referenced this pull request Feb 10, 2025
This patch reverts commit [1] and documents it.

The problem is after [1] the following code

    const fromRegularResult = (Ok(1) as Result<number, string>).toAsyncResult();
    fromRegularResult.andThen((value) => Ok(value * 2));

would failt co compile:

    src/file.ts:169:19 - error TS2349: This expression is not callable.
      Each member of the union type '{ <T2>(mapper: (val: never) => Ok<T2> | Promise<Ok<T2>> | AsyncResult<T2, never>): AsyncResult<T2, string>; <T2, E2>(mapper: (val: never) => Result<...> | ... 1 more ... | AsyncResult<...>): AsyncResult<...>; } | { ...; }' has signatures, but none of those signatures are compatible with each other.

    169 fromRegularResult.andThen((value) => Ok(value * 2));
                          ~~~~~~~

    src/file.ts:169:28 - error TS7006: Parameter 'value' implicitly has an 'any' type.

    169 fromRegularResult.andThen((value) => Ok(value * 2));
                                   ~~~~~

I spend the last Friday's afternoon trying to figure out why and how to
fix it and I failed. I think reverting the original patch is the way to
go for now and it was handling an edge anyway.

It's mort important that andThen works in general cases (where the
callbacks return Result, not always Ok).

[1] 9259f83 ("Fix the AsyncResult.andThen handling of known-Ok transformations (#189)")
jstasiak added a commit that referenced this pull request Feb 10, 2025
This patch reverts commit [1] and documents this fact.

The problem is after [1] the following code

    const fromRegularResult = (Ok(1) as Result<number, string>).toAsyncResult();
    fromRegularResult.andThen((value) => Ok(value * 2));

would fail co compile:

    src/file.ts:169:19 - error TS2349: This expression is not callable.
      Each member of the union type '{ <T2>(mapper: (val: never) => Ok<T2> | Promise<Ok<T2>> | AsyncResult<T2, never>): AsyncResult<T2, string>; <T2, E2>(mapper: (val: never) => Result<...> | ... 1 more ... | AsyncResult<...>): AsyncResult<...>; } | { ...; }' has signatures, but none of those signatures are compatible with each other.

    169 fromRegularResult.andThen((value) => Ok(value * 2));
                          ~~~~~~~

    src/file.ts:169:28 - error TS7006: Parameter 'value' implicitly has an 'any' type.

    169 fromRegularResult.andThen((value) => Ok(value * 2));
                                   ~~~~~

I spent the last Friday's afternoon trying to figure out why and how to
fix it and I failed. I think reverting the original patch is the way to
go for now and it was handling an edge case anyway.

It's more important that andThen works in general cases (where the
callbacks return Result, not always Ok).

[1] 9259f83 ("Fix the AsyncResult.andThen handling of known-Ok transformations (#189)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AsyncResult.andThen return type behaves weirdly sometimes
2 participants