Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type "unknown" for the reason in Promise.catch callback in lib.es5.d.ts #57270

Closed
etonee123x opened this issue Feb 2, 2024 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@etonee123x
Copy link

etonee123x commented Feb 2, 2024

⚙ Compilation target

esnext

⚙ Library

lib.es5

Missing / Incorrect Definition

Promise.catch

Important:

strict mode is enabled. "useUnknownInCatchVariables" is not disabled.

Description

Check the sample code. It represents some fetch-request (0) and transformation of its results (1). For some reasons (inattention) I return error in Promise.catch (3).

What do I expect?

I expect that error (4.0) type (and then data (4.1) type) will be of type "unknown" and IDE will underline spreading data at transformation (1), typechecks will fail, etc --> I will notice the error --> I will prevent the error in runtime.

What do I get?

Error (4.0) type (and then data (4.1) type) are "any" and IDE didn't underline the error, typechecks didn't fail, etc --> I didn't notice the error --> I got TypeError in runtime, cos the error value (5) is not iterable.

Investigation

In other issue, where author was requesting for generic for rejected promise values, rbuckton agreed that type "any" is the most accurate type for rejected promises values. It was ~2-3 years before type "unknown" was introduced, though 🤔!..

Potential solution

Of course, I can add type annotation "error: unknown" in catch callback on my own, but I think it should be automatically inferred with flags I mentioned before. Or maybe create a new flag for this case. Or maybe change Promise.catch definition. Or maybe I don't understand something, please explain me!

Sample Code

// (0)
const getData = () => new Promise<Array<number>>(
  (resolve, reject) => Math.random()
    ? resolve([1, 2, 3])
    : reject(new Error('error!')), // (5)
);

(async () => {
  const data = await getData() // (4.1)
    .catch((error) => { // (4.0)
      console.log(error);
      return error; // (3)
    });

  return [0, ...data]; // (1)
})();

Documentation Link

No response

@MartinJohns
Copy link
Contributor

Duplicate of #45602. Used search terms: promise catch in:title

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 2, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants