Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wardbell opened this issue Sep 21, 2015 · 4 comments
Assignees
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue

Comments

@wardbell
Copy link

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>.

@jocull
Copy link

jocull commented Jul 11, 2016

+1. This is worse because for lack of better type information it becomes Promise<any>. Sneakily destroying your other typing information along the way. (making them all any)

@mhegazy
Copy link
Contributor

mhegazy commented Jul 11, 2016

// CC: @rbuckton

@RyanCavanaugh
Copy link
Member

Ping @rbuckton ?

@rbuckton
Copy link
Member

While the results of the above scenario have changed since the introduction of strictNullChecks and other recent improvements to the type checker, there are still some issues with the resulting types in the above scenarios.

I'm investigating improvements to the Promise type declarations to see what could help.

rbuckton added a commit that referenced this issue Aug 20, 2016
@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript labels Aug 22, 2016
@mhegazy mhegazy added this to the TypeScript 2.1 milestone Aug 27, 2016
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Sep 14, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

5 participants