You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Promise catch error parameter should not be of type any. #6283
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:
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});
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.
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 😄 .
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.
Activity
DanielRosenwasser commentedon Dec 29, 2015
Ron Buckton (@rbuckton) can you weigh in here?
ivogabe commentedon Dec 29, 2015
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
anywould be the most accurate type.tinganho commentedon Dec 29, 2015
Ivo Gabe de Wolff (@ivogabe) maybe it could be better handled with a union of type
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.
tinganho commentedon Dec 29, 2015
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 😄 .zpdDG4gta8XKpMCd commentedon Dec 30, 2015
anything united with
anycannot be differentiated, if it hasanyit will stayanytry:
tinganho commentedon Dec 31, 2015
Hmm strange,
typeofguards works.tinganho commentedon Dec 31, 2015
Just reported it here #6301.
rbuckton commentedon Aug 18, 2016
Ivo Gabe de Wolff (@ivogabe) is correct. The exception is typed
anybecause 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.