-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Flag to type Promise.catch
variables as unknown
#45602
Comments
The type for |
You can easily achieve this by overloading the signature of // 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>;
} |
@Cellule Thanks for the snippet, yes, you can do this yourself, but the goal of this suggestion is to add this to TS itself |
@Cellule thanks for that! Unfortunately it doesn't cover interface PromiseRejectedResult {
status: "rejected";
// @ts-expect-error TS2717 even if overridden, it doesn't work
reason: unknown;
} |
I tried this, but it seems like TS just selects the 'any' overload if the signatures don't match. Playground |
I'd really like to see this implemented, and I think it's a shame it wasn't considered for release alongside
Now, if |
Are not the same, since if
async p => {
let result;
try {
result = await p;
} catch (err) {
return bad(err);
}
return good(result);
} |
That's correct, but I didn't mean that those forms were identical, I was only referring to the parameter type of the |
Could the approach used to fix iterator types in #58243 be used to fix Similiar to the interface Promise<T> {
catch<TResult = never>(onrejected?: ((reason: PromiseCatchErrorType) => TResult | PromiseLike<TResult>) | undefined | null): Promise<T | TResult>;
} |
Probably obvious, but slight extension to @Cellule's Promise type to also infer // Promise.d.ts
interface Promise<T> {
then<TFul, TRej = never>(
onfulfilled?: ((value: T) => TFul | PromiseLike<TFul>) | null,
onrejected?: ((reason: unknown) => TRej | PromiseLike<TRej>) | null,
): Promise<TFul | TRej>
catch<TRej = never>(
onrejected?: ((reason: unknown) => TRej | PromiseLike<TRej>) | null,
): Promise<T | TRej>
} Node 20.12, TS ^5.7.2 w/ target & lib = ESNext |
Suggestion
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
Following #41016 a flag was added to consider
catch
variables asunknown
.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
The text was updated successfully, but these errors were encountered: