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 error in Promise catch methods as unknown #18

Merged
merged 7 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .changeset/tender-socks-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
"@total-typescript/ts-reset": minor
---

Added a rule, `/promise-catch`, to change the `catch` method to take `unknown` instead of `any` as an argument.

```ts
const promise = Promise.reject("error");

// BEFORE

promise.catch((error) => {
console.error(error); // error is any!
});

// AFTER

promise.catch((error) => {
console.error(error); // error is unknown!
});
```
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
"import": "./dist/set-has.mjs",
"default": "./dist/set-has.js"
},
"./promise-catch": {
"types": "./dist/promise-catch.d.ts",
"import": "./dist/promise-catch.mjs",
"default": "./dist/promise-catch.js"
},
"./map-constructor": {
"types": "./dist/map-constructor.d.ts",
"import": "./dist/map-constructor.mjs",
Expand Down
30 changes: 30 additions & 0 deletions src/entrypoints/promise-catch.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
interface Promise<T> {
/**
* Attaches callbacks for the resolution and/or rejection of the Promise.
* @param onfulfilled The callback to execute when the Promise is resolved.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of which ever callback is executed.
*/
then<TResult1 = T, TResult2 = never>(
onfulfilled?:
| ((value: T) => TResult1 | PromiseLike<TResult1>)
| undefined
| null,
onrejected?:
| ((reason: unknown) => TResult2 | PromiseLike<TResult2>)
| undefined
| null,
): Promise<TResult1 | TResult2>;

/**
* 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>;
}
1 change: 1 addition & 0 deletions src/entrypoints/recommended.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/// <reference path="map-constructor.d.ts" />
/// <reference path="map-has.d.ts" />
/// <reference path="array-index-of.d.ts" />
/// <reference path="promise-catch.d.ts" />
14 changes: 14 additions & 0 deletions src/tests/promise-catch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { doNotExecute, Equal, Expect } from "./utils";

doNotExecute(async () => {
Promise.reject("string instead of Error").catch((err) => {
type test = Expect<Equal<unknown, typeof err>>;
});

Promise.reject("string instead of Error").then(
() => {},
(err) => {
type test = Expect<Equal<unknown, typeof err>>;
},
);
});
Loading