Skip to content

Commit

Permalink
Merge pull request #18 from DavidANeil/davidneil-promise-catch
Browse files Browse the repository at this point in the history
Type error in Promise catch methods as unknown
  • Loading branch information
mattpocock authored May 27, 2024
2 parents 073a6b9 + 5bf3a15 commit 2d02b35
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
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>>;
},
);
});

0 comments on commit 2d02b35

Please sign in to comment.