Skip to content

Commit fdb1d16

Browse files
feat: add redux-toolkit-floating-promises example
1 parent 2d41579 commit fdb1d16

File tree

6 files changed

+191
-3
lines changed

6 files changed

+191
-3
lines changed

package-lock.json

+89-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Example: `redux-toolkit` and Floating Promise Detection
2+
3+
An example of using [`redux-toolkit`](https://redux-toolkit.js.org) along with the [`@typescript-eslint/no-floating-promises` rule](https://typescript-eslint.io/rules/no-floating-promises) enabled.
4+
It uses the [`allowForKnownSafePromises` rule option](https://typescript-eslint.io/rules/no-floating-promises/#allowforknownsafepromises) to not report on RTK APIs that create a `SafePromise`, such as `createAsyncThunk`.
5+
6+
## Setup
7+
8+
```shell
9+
npm i
10+
```
11+
12+
## Usage
13+
14+
```shell
15+
npm run lint
16+
```
17+
18+
There should be no lint reports.
19+
20+
If you remove the `allowForKnownSafePromises` option from `eslint.config.js`, there will be:
21+
22+
```plaintext
23+
.../index.test.ts
24+
4:1 error Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator @typescript-eslint/no-floating-promises
25+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// @ts-check
2+
3+
import eslint from "@eslint/js";
4+
import tseslint from "typescript-eslint";
5+
6+
export default tseslint.config(
7+
eslint.configs.recommended,
8+
tseslint.configs.recommendedTypeChecked,
9+
{
10+
languageOptions: {
11+
parserOptions: {
12+
projectService: {
13+
allowDefaultProject: ['*.config.*']
14+
},
15+
},
16+
},
17+
rules: {
18+
"@typescript-eslint/no-floating-promises": [
19+
"error", {
20+
"allowForKnownSafePromises": [
21+
{ "from": "package", "name": "SafePromise", "package": "@reduxjs/toolkit" }
22+
]
23+
}
24+
]
25+
}
26+
}
27+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { configureStore, createAsyncThunk } from '@reduxjs/toolkit';
2+
3+
function reducer() {
4+
// ...
5+
}
6+
7+
const store = configureStore({ reducer });
8+
9+
const exampleThunk = createAsyncThunk(
10+
'example',
11+
async () => {
12+
// ...
13+
},
14+
);
15+
16+
store.dispatch(exampleThunk());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "redux-toolkit-floating-promises",
3+
"version": "0.0.0",
4+
"description": "Example of using `redux-toolkit` with `@typescript-eslint/no-floating-promises`'s `allowForKnownSafePromises` option.",
5+
"main": "index.ts",
6+
"repository": {
7+
"directory": "packages/redux-toolkit-floating-promises",
8+
"type": "git",
9+
"url": "https://github.com/typescript-eslint/typescript-eslint-examples"
10+
},
11+
"license": "MIT",
12+
"devDependencies": {
13+
"@types/node": "^22.10.7",
14+
"eslint": "^9.16.0",
15+
"typescript": "^5.7.2",
16+
"typescript-eslint": "^8.18.0"
17+
},
18+
"dependencies": {
19+
"@reduxjs/toolkit": "2.5.0"
20+
},
21+
"scripts": {
22+
"lint": "eslint .",
23+
"tsc": "tsc"
24+
},
25+
"type": "module"
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"module": "NodeNext",
4+
"moduleResolution": "NodeNext",
5+
"noEmit": true,
6+
"strict": true
7+
},
8+
}

0 commit comments

Comments
 (0)