Skip to content

Different types for rejected/fulfilled Promise #7588

@OliverJAsh

Description

@OliverJAsh

TypeScript 1.8.9

I'm trying to understand how to correctly type a rejected promise. I expect the following code to compile.

const bar: Promise<number> =
    Promise.resolve(1)
        .then(() => Promise.reject<string>(new Error('foo')))

Error:

main.ts(7,7): error TS2322: Type 'Promise<string>' is not assignable to type 'Promise<number>'.
  Type 'string' is not assignable to type 'number'.

Is it possible to have different types for a Promise when it is rejected and fulfilled?

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions