Skip to content

somePromise.catch or .then(null, rejectFn) should preserve type of original promise but doesn't #4903

@wardbell

Description

@wardbell

The fundamental point is that the catch of a promise should NOT change the type of that promise unless it returns something different. Returning something different is relatively rare because the activation of the catch should be rare.

Because catch is sugar over then(null, rejectFn), that form has the same issue.

Example 1: foo returns Promise<Hero>

    foo(){
        return Promise.resolve(new Hero('foo'));
    }

Example 2: foo returns a Promise<{}> ! This is AWFUL

    foo(){
        return Promise.resolve(new Hero('foo'))
                  // Pick any of these to see the problem
          .catch()
          .catch(e => {})
          .then(null, e => {})
    }

Example 3: foo returns a Promise<{}> ! This is AWFUL TOO

    foo(){
        return Promise.resolve(new Hero('foo'))
                  // Pick either of these to see the problem
          .catch(e => Promise.reject(e))
          .then(null, e => Promise.reject(e))
    }

Example 4: foo returns a Promise<Hero> ... but this is baroque

    foo(){
        return Promise.resolve(new Hero('foo'))
          .then(_ => _, e => Promise.reject(e));
    }

The problem is in the typings file I think. I contend that that catch or then(null, rejectFn) should be typed to return a Promise<Hero>.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions