-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Promise catch error parameter should not be of type any. #6283
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
@rbuckton can you weigh in here? |
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 somePromise.then(x => {
throw "foo";
}).catch(error => {
// error can also be a string here
}); |
@ivogabe maybe it could be better handled with a union of type .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. |
But after think about it. Maybe better to wait until default type argument lands in #2175? So that default error type is |
anything united with try: function isString(value: string | any) : value is string {
return typeof value === 'string';
}
let value : string | any;
if (isString(value)) {
value // <-- check me
} |
Hmm strange, let value : string | any;
if ( typeof value === 'string') {
value // <-- check me
} |
Just reported it here #6301. |
@ivogabe is correct. The exception is typed |
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 justPromise<R>
.I need to type annotate many catch method calls right now with:
The text was updated successfully, but these errors were encountered: