Skip to content

Promise catch error parameter should not be of type any. #6283

Closed

Description

@tinganho

Is there any chance, you could change the any type on error parameter on the catch method of a Promise, to a type argument instead? The type argument is provided in the Promise<R, E> type annotation, instead of just Promise<R> .

    /**
     * Sugar for promise.then(undefined, onRejected)
     *
     * @param onRejected called when/if "promise" rejects
     */
    catch<U>(onRejected?: (error: any) => U | Thenable<U>): Promise<U>;

I need to type annotate many catch method calls right now with:

.catch(error:  HTTPResponse<ErrorResponse> | Error)

Activity

  1. DanielRosenwasser commented on Dec 29, 2015

    @DanielRosenwasser
    Member

    Ron Buckton (@rbuckton) can you weigh in here?

  2. ivogabe commented on Dec 29, 2015

    @ivogabe
    Contributor

    I think it's not possible to type that accurately, as errors in a previous listener will be passed to the catch listener. Functions are not annotated with their possible errors, so the error could be everything, so any would be the most accurate type.

    somePromise.then(x => {
      throw "foo";
    }).catch(error => {
      // error can also be a string here
    });
  3. tinganho commented on Dec 29, 2015

    @tinganho
    ContributorAuthor

    Ivo Gabe de Wolff (@ivogabe) maybe it could be better handled with a union of type any?

    .catch(error:  HTTPResponse<ErrorResponse> | any)

    Promise rejection are not exceptions only. you consciously reject sometimes. And I just think it is a good idea that people can handle those conscious rejections, i.e. give it a type.

  4. tinganho commented on Dec 29, 2015

    @tinganho
    ContributorAuthor

    But after think about it. Maybe better to wait until default type argument lands in #2175? So that default error type is any. It also don't break anyones code then 😄 .

  5. zpdDG4gta8XKpMCd commented on Dec 30, 2015

    @zpdDG4gta8XKpMCd

    anything united with any cannot be differentiated, if it has any it will stay any

    try:

    function isString(value: string | any) : value is string {
        return typeof value === 'string';
    }
    let value : string | any;
    if (isString(value)) {
        value // <-- check me
    }
  6. tinganho commented on Dec 31, 2015

    @tinganho
    ContributorAuthor

    Hmm strange, typeof guards works.

    let value : string | any;
    if ( typeof value === 'string') {
        value // <-- check me
    }
  7. tinganho commented on Dec 31, 2015

    @tinganho
    ContributorAuthor

    Just reported it here #6301.

  8. added
    Working as Intended
    on Aug 18, 2016
  9. rbuckton commented on Aug 18, 2016

    @rbuckton
    Contributor

    Ivo Gabe de Wolff (@ivogabe) is correct. The exception is typed any because we cannot guarantee the correct type of the exception at design time, and neither TypeScript nor JavaScript provide the ability to guard exception types at run time. Your best option is to use type guards to provide both a design-time and run-time check in your code.

  10. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Working as Intended

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions