Skip to content

Problem returning union of promises in then callback #27562

@ghost

Description

TypeScript Version: 3.2.0-dev.20181004

Code

function f(b: boolean, pn: Promise<number>, ps: Promise<string>) {
 return Promise.resolve().then(() => b ? pn : ps);
}

Expected behavior:

No error.

Actual behavior:

Pyramid of doom error message:

src/a.ts:2:35 - error TS2345: Argument of type '() => Promise<string> | Promise<number>' is not assignable to parameter of type '(value: void) => string | PromiseLike<string>'.
  Type 'Promise<string> | Promise<number>' is not assignable to type 'string | PromiseLike<string>'.
    Type 'Promise<number>' is not assignable to type 'string | PromiseLike<string>'.
      Type 'Promise<number>' is not assignable to type 'PromiseLike<string>'.
        Types of property 'then' are incompatible.
          Type '<TResult1 = number, TResult2 = never>(onfulfilled?: ((value: number) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined) => Promise<...>' is not assignable to type '<TResult1 = string, TResult2 = never>(onfulfilled?: ((value: string) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined) => PromiseLike<...>'.
            Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
              Types of parameters 'value' and 'value' are incompatible.
                Type 'number' is not assignable to type 'string'.

2     return Promise.resolve().then(() => b ? pn : ps);

Related Issues: #20531

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions