Flag to type `Promise.catch` variables as `unknown`
See original GitHub issueSuggestion
✅ Viability Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript/JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
⭐ Suggestion
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.
See also: eslint-plugin-etc/no-implicit-any-catch
Issue Analytics
- State:
- Created 2 years ago
- Reactions:8
- Comments:8 (2 by maintainers)
Top Results From Across the Web
How to do try catch and finally statements in TypeScript?
Update: TypeScript 4.4 provides a config flag --useUnknownInCatchVariables to let catch-variables default to type unknown .
Read more >Catching Unknowns — @ncjamieson
Here, with the catch -clause variable typed as unknown , a type guard is necessary to establish whether or not the caught value...
Read more >Promise.prototype.catch() - JavaScript - MDN Web Docs
Returns a new Promise . This new promise is always pending when returned, regardless of the current promise's status. It's eventually rejected ...
Read more >Announcing TypeScript 4.4 - Microsoft Developer Blogs
Users running with the --strict flag may see new errors around catch variables being unknown , especially if the existing code assumes only ......
Read more >Documentation - Advanced Types - TypeScript
Advanced concepts around types in TypeScript. ... The strictNullChecks flag fixes this: when you declare a variable, ... type T4 = Promise<string>.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You can easily achieve this by overloading the signature of
Promise.catch
in your project@Cellule Thanks for the snippet, yes, you can do this yourself, but the goal of this suggestion is to add this to TS itself