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

orElse returns the wrong type when the callback changes the ok type #437

Open
lantw44 opened this issue Dec 13, 2022 · 1 comment · May be fixed by #484
Open

orElse returns the wrong type when the callback changes the ok type #437

lantw44 opened this issue Dec 13, 2022 · 1 comment · May be fixed by #484

Comments

@lantw44
Copy link

lantw44 commented Dec 13, 2022

Result is broken. It doesn't track ok type changes.

import { Result, ok, err } from 'neverthrow';

const a: Result<number, number> = err(1);
const b: Result<number, number> = a.orElse(() => ok('A'));
console.log(b);
$ npx tsc
$ node main.js
Ok { value: 'A' }

The ok type of b is string, but TypeScript thinks it is number.

ResultAsync is fine. It rejects ok type changes.

import { ResultAsync, okAsync, errAsync } from 'neverthrow';

const a: ResultAsync<number, number> = errAsync(1);
const b: ResultAsync<number, number> = a.orElse(() => okAsync('A'));
b.then((r) => console.log(r));
$ npx tsc
main.ts:4:7 - error TS2322: Type 'ResultAsync<number, unknown>' is not assignable to type 'ResultAsync<number, number>'.
  Type 'unknown' is not assignable to type 'number'.

4 const b: ResultAsync<number, number> = a.orElse(() => okAsync('A'));
        ~

main.ts:4:55 - error TS2769: No overload matches this call.
  Overload 1 of 3, '(f: (e: number) => Result<number, unknown>): ResultAsync<number, unknown>', gave the following error.
    Type 'ResultAsync<string, never>' is not assignable to type 'Result<number, unknown>'.
      Type 'ResultAsync<string, never>' is missing the following properties from type 'Err<number, unknown>': error, isOk, isErr, asyncAndThen, and 3 more.
  Overload 2 of 3, '(f: (e: number) => ResultAsync<number, unknown>): ResultAsync<number, unknown>', gave the following error.
    Type 'ResultAsync<string, never>' is not assignable to type 'ResultAsync<number, unknown>'.
      Type 'string' is not assignable to type 'number'.
  Overload 3 of 3, '(f: (e: number) => Result<number, never> | ResultAsync<number, never>): ResultAsync<number, never>', gave the following error.
    Type 'ResultAsync<string, never>' is not assignable to type 'Result<number, never> | ResultAsync<number, never>'.
      Type 'ResultAsync<string, never>' is not assignable to type 'ResultAsync<number, never>'.
        Type 'string' is not assignable to type 'number'.

4 const b: ResultAsync<number, number> = a.orElse(() => okAsync('A'));
                                                        ~~~~~~~~~~~~

  node_modules/neverthrow/dist/index.d.ts:19:45
    19     orElse<R extends Result<T, unknown>>(f: (e: E) => R): ResultAsync<T, InferErrTypes<R>>;
                                                   ~~~~~~~~~~~
    The expected type comes from the return type of this signature.
  node_modules/neverthrow/dist/index.d.ts:20:50
    20     orElse<R extends ResultAsync<T, unknown>>(f: (e: E) => R): ResultAsync<T, InferAsyncErrTypes<R>>;
                                                        ~~~~~~~~~~~
    The expected type comes from the return type of this signature.
  node_modules/neverthrow/dist/index.d.ts:21:18
    21     orElse<A>(f: (e: E) => Result<T, A> | ResultAsync<T, A>): ResultAsync<T, A>;
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The expected type comes from the return type of this signature.


Found 2 errors in the same file, starting at: main.ts:4
@Chuckytuh
Copy link

This seems related with #411

@braxtonhall braxtonhall linked a pull request Jun 29, 2023 that will close this issue
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 a pull request may close this issue.

2 participants