-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Promise rejection type. #39680
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
Comments
Looks like duplicate of #6283 and other similar. |
I don't feel like they rejection type always has to be of type It is obviously possible to detect what the rejection type is when I've been having a bit of a play with this to see what it would be like add support. This is what I've got so far just by playing with type definitions. |
p2 // p2 is of type Promise<number, string>
.then((v2) => void cosnole.log(v2)) // v2 is of type number
.catch((e2) => void console.error(e2)); // e2 is actually type string | ReferenceError, not string |
Why would the type be |
Yes, TS will report 2304 in this obvious case however emitted JS code will still have a change to receive both Looks like |
Could you give a small example? If |
In unsound cases or unknown external code (e.g. declare const computedBoolean: (params: any) => boolean; // but in general case might throw
const list = [{id: 111}, {id: 222}];
function firstThreeIds(data: Array<{id: number}>): number[] {
return [
data[0].id,
data[1].id,
data[2].id // no compile time error, but will throw
];
}
const p = new Promise<number[], string>((resolve, reject) => {
if (computedBoolean(list)) { // can throw before resolve
resolve(firstThreeIds(list)); // wil throw before resolve
} else {
reject("foo");
}
});
p
.catch((e) => { /* ... */}); // actually string | TypeError So it looks like closest thing |
Could this proposal solve this issue? |
Promise rejection errors as jsdoc, because it is not supported in TS yet (microsoft/TypeScript#39680). Use Generics to type resolved value and message event data.
Promise rejection errors as jsdoc, because it is not supported in TS yet (microsoft/TypeScript#39680). Use Generics to type resolved value and message event data.
I just opened #45869. I think maybe the best-case outcome would be implementing #13219 (throws-clause) and this issue, in which case it would become possible to document Promises that have a It would be a lot of work for most projects to migrate to throws-clause compliance but once done, the type checker would be able to reason about exception / rejection paths -- I would love to automate that reasoning, rather than getting bitten by orphaned async-function calls and having to hunt them down to fix on my own. |
I'm confused, is this implemented or not? Can't find any info in google. |
It's not. The rejection type is always |
Just encountered working with In both situations, it seems that the appropriate type should be |
Since this change isn't breaking you could always monkey patch the appropriate .d.ts file with the proposed change. I think it would be nice if this made it into the official typings. There still other changes that would need to be made to the compiler to get a good developer experience. For instance, if you |
I don't understand the argument that the rejection type can't be guaranteed. And yes maybe in the |
If you read the issues I linked from my previous post (2 years ago!), you'll see some of the reasoning. I think one of the most compelling arguments against allowing manual declaration of a throws or rejects type is that a novice who reads your code is unlikely to realize that you're making an unsafe assertion, along the lines of a typecast. I'd still really like to see a way to annotate Promises that cannot reject, though. I actually write a lot of async functions with top level try/catch - it's a good pattern to follow when implementing an Express router callback, since the server library will not handle a rejection for you. |
In this case is the developer that set the return type the one that needs to guarantee the thrown exception is the correct type. I'm telling typescript I will only reject with that kind of type. In typescript is perfectly valid to write function sum(a: number, b: number): number {
return 'hello world' as any as number;
} This is totally valid TS although would fail at runtime, don't see why the developer could not explicitly tells typescript he will make sure the rejected value is a certain type. I'm not asking typescript to check thrown types etc. just so the consumers of the promises know the rejected type. |
Sorry I wasn't more specific, but I wrote the previous comment from a phone. I was referring to #45869 (comment) :
The point was that, if rejection-types were implemented, I could write async function lookup(key: string): Promise<string, never> {
try {
return maybeGetValueEventually(key);
} catch {
logThatMightThrow(key);
return "fallback";
}
} but without comprehensive checked exceptions (a feature that has already been rejected) the type-checker couldn't prove that the function never rejects. In this example, the first generic argument in the returned-value Promise type ( I do think this could be handled by a linter rule ("all explicit Promise rejection types require a comment"), and I would still argue that some construct for asserting at its source that a Promise cannot reject is useful enough to merit a certain amount of novice-developer hazard, but I wanted to point out that there is a totally reasonable counterargument here. |
Search Terms
Promise Reject Type
Suggestion
Add ability to type Promise rejections.
Use Cases
When handling promise rejections, the type
any
isn't very useful. I would be useful to have the rejection have an actual type without the need to cast or type guard.Examples
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: