-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from DavidANeil/davidneil-promise-catch
Type error in Promise catch methods as unknown
- Loading branch information
Showing
5 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>>; | ||
}, | ||
); | ||
}); |