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
Flag to type Promise.catch variables as unknown #45602
Following #41016 a flag was added to consider catch variables as unknown.
For consistency and since they're similar cases, I suggest also adding a flag that does this for .catch callbacks on promises.
The type for .catch(...) callback parameters is defined in lib files, so we’d have to expose a new type that acts like unknown or any depending on compilation settings... and I’m not sure we’d want that type escaping into the wild 😬. This is a compelling use case, though.
You can easily achieve this by overloading the signature of Promise.catch in your project
// Promise.d.tsinterfacePromise<T>{/** * Attaches a callback for only the rejection of the Promise. * @param onrejected The callback to execute when the Promise is rejected. * @returns A Promise for the completion of the callback. */catch<TResult=never>(onrejected?: ((reason: unknown)=>TResult|PromiseLike<TResult>)|undefined|null,): Promise<T|TResult>;}
Reacted by Andrew Branch, fregante, Manuel Dugué, ExE Boss, zanminkian, Tor Gunnar Høst Houeland, BlueGreenMagick, rivatove and Dilettante
I'd really like to see this implemented, and I think it's a shame it wasn't considered for release alongside useUnknownInCatchVariables. Before the flag existed, you could always freely convert between
p.then(good, bad)
p.then(good).catch(bad), and
try { result = await p; good(result) } catch (err) { bad(err); } (in async context)
Now, if bad doesn't expect unknown it will fail, but only in the last of these forms. I want a flag that says "use unknown type for all errors", including try/catch and Promise rejection -- which are the same thing, under async/await!
That's correct, but I didn't mean that those forms were identical, I was only referring to the parameter type of the bad function. The point is that the then-callback gets any while the catch-block invocation gets unknown.
Could the approach used to fix iterator types in #58243 be used to fix promise.catch as well?
Similiar to the BuiltinIteratorReturn type, perhaps a PromiseCatchErrorType can be used that would be either any or unknown depending on a compiler flag.
Activity
andrewbranch commentedon Aug 31, 2021
The type for
.catch(...)callback parameters is defined in lib files, so we’d have to expose a new type that acts likeunknownoranydepending on compilation settings... and I’m not sure we’d want that type escaping into the wild 😬. This is a compelling use case, though.Cellule commentedon Oct 5, 2021
You can easily achieve this by overloading the signature of
Promise.catchin your projectyoav-lavi commentedon Oct 5, 2021
Michael "Mike" Ferris (@Cellule) Thanks for the snippet, yes, you can do this yourself, but the goal of this suggestion is to add this to TS itself
fregante commentedon Dec 6, 2021
Michael "Mike" Ferris (@Cellule) thanks for that! Unfortunately it doesn't cover
allSettledthough and I wasn't able to override that:https://www.typescriptlang.org/play?target=7#code/IYZwngdgxgBAZgV2gFwJYHsIwG4FMBOYehEqUuAFAJQwDeAUDEzFJiMjAA77oC2qIXCBgBeGMADuwVBwAKPfoIB0wADaqAyrmTJVuACYUA2gEYAulUbM46fDAqsI7LgoG4Y6OC75uQNBszMqF4U3D7K7MDICMIicTAARPi4AFa4UMgGCf5WgUxhirhKyaCYucwAvuVVFUA
emartinfigma commentedon Feb 11, 2022
I tried this, but it seems like TS just selects the 'any' overload if the signatures don't match. Playground
thw0rted commentedon Apr 6, 2022
I'd really like to see this implemented, and I think it's a shame it wasn't considered for release alongside
useUnknownInCatchVariables. Before the flag existed, you could always freely convert betweenp.then(good, bad)p.then(good).catch(bad), andtry { result = await p; good(result) } catch (err) { bad(err); }(inasynccontext)Now, if
baddoesn't expectunknownit will fail, but only in the last of these forms. I want a flag that says "useunknowntype for all errors", including try/catch and Promise rejection -- which are the same thing, under async/await!ExE-Boss commentedon Apr 10, 2022
James Bromwell (@thw0rted)
Are not the same, since if
goodthrows an error, only the latter form will catch it.p => p.then(good, bad)is actually equivalent to:thw0rted commentedon Apr 10, 2022
That's correct, but I didn't mean that those forms were identical, I was only referring to the parameter type of the
badfunction. The point is that the then-callback getsanywhile the catch-block invocation getsunknown.NotWoods commentedon Jul 26, 2024
Could the approach used to fix iterator types in #58243 be used to fix
promise.catchas well?Similiar to the
BuiltinIteratorReturntype, perhaps aPromiseCatchErrorTypecan be used that would be eitheranyorunknowndepending on a compiler flag.jfxio commentedon Jan 6, 2025
Probably obvious, but slight extension to Michael "Mike" Ferris (@Cellule)'s Promise type to also infer
unknownfor theonrejectedthen()parameter. This covers bothbaduses in James Bromwell (@thw0rted)'s reply.Node 20.12, TS ^5.7.2 w/ target & lib = ESNext