Skip to content

Improve ResultAsync.map* method types #616

Open
@rijenkii

Description

@rijenkii

Those functions cannot receive callbacks that return type A | Promise<B>.

Reproduction for map (playground):

declare class ResultAsync<T, E> {
    map<A>(f: (t: T) => A | Promise<A>): ResultAsync<A, E>;
}

declare function mapper(x: string): number | Promise<string>;
declare const x: ResultAsync<string, never>;

const result = x.map(mapper);
// Argument of type '(x: string) => number | Promise<string>' is not assignable to parameter of type '(t: string) => string | Promise<string>'.
//   Type 'number | Promise<string>' is not assignable to type 'string | Promise<string>'.
//     Type 'number' is not assignable to type 'string | Promise<string>'.(2345)

A naïve solution that I came up with looks like this (playground):

declare class ResultAsync<T, E> {
  map<A>(f: (t: T) => A): ResultAsync<Awaited<A>, E>;
}

declare function mapper(x: string): number | Promise<string>;
declare const x: ResultAsync<string, never>;

const result = x.map(mapper);
//    ^? const result: ResultAsync<string | number, never>

This also applies to mapErr and potentially to then, but I have not tested that one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions