question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Flag to type `Promise.catch` variables as `unknown`

See original GitHub issue

Suggestion

✅ 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:open
  • Created 2 years ago
  • Reactions:8
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
Cellulecommented, Oct 5, 2021

You can easily achieve this by overloading the signature of Promise.catch in your project

// Promise.d.ts
interface Promise<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>;
}
2reactions
yoav-lavicommented, Oct 5, 2021

@Cellule Thanks for the snippet, yes, you can do this yourself, but the goal of this suggestion is to add this to TS itself

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found