microsoft / microsoft/TypeScript

Problem returning union of promises in `then` callback

Open
#27,562 1 comment 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Proposal Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in src/a.ts at line 2 and compare the behavior with related issue #20531. Investigate how the Promise.resolve().then callback is typed for a union of promises; done means the shown code compiles without an error while retaining the expected promise result types.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.