Skip to content

feat: add redux-toolkit-floating-promises example #20

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
92 changes: 89 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions packages/redux-toolkit-floating-promises/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Example: `redux-toolkit` and Floating Promise Detection

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.
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`.

## Setup

```shell
npm i
```

## Usage

```shell
npm run lint
```

There should be no lint reports.

If you remove the `allowForKnownSafePromises` option from `eslint.config.js`, there will be:

```plaintext
.../index.test.ts
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
```
29 changes: 29 additions & 0 deletions packages/redux-toolkit-floating-promises/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// @ts-check

import eslint from "@eslint/js";
import tseslint from "typescript-eslint";

export default tseslint.config(
eslint.configs.recommended,
tseslint.configs.recommendedTypeChecked,
{
languageOptions: {
parserOptions: {
projectService: {
allowDefaultProject: ['*.config.*']
},
},
},
rules: {
"@typescript-eslint/no-floating-promises": [
"error", {
"allowForKnownSafePromises": [
{ from: "package", name: "SafePromise", package: "@reduxjs/toolkit" },
{ from: 'package', name: 'SafePromise', package: '@reduxjs/toolkit/query' },
{ from: 'package', name: 'SafePromise', package: '@reduxjs/toolkit/query/react' },
]
}
]
}
}
);
16 changes: 16 additions & 0 deletions packages/redux-toolkit-floating-promises/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { configureStore, createAsyncThunk } from '@reduxjs/toolkit';

function reducer() {
// ...
}

const store = configureStore({ reducer });

const exampleThunk = createAsyncThunk(
'example',
async () => {
// ...
},
);

store.dispatch(exampleThunk());
26 changes: 26 additions & 0 deletions packages/redux-toolkit-floating-promises/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "redux-toolkit-floating-promises",
"version": "0.0.0",
"description": "Example of using `redux-toolkit` with `@typescript-eslint/no-floating-promises`'s `allowForKnownSafePromises` option.",
"main": "index.ts",
"repository": {
"directory": "packages/redux-toolkit-floating-promises",
"type": "git",
"url": "https://github.com/typescript-eslint/typescript-eslint-examples"
},
"license": "MIT",
"devDependencies": {
"@types/node": "^22.10.7",
"eslint": "^9.16.0",
"typescript": "^5.7.2",
"typescript-eslint": "^8.18.0"
},
"dependencies": {
"@reduxjs/toolkit": "2.5.0"
},
"scripts": {
"lint": "eslint .",
"tsc": "tsc"
},
"type": "module"
}
8 changes: 8 additions & 0 deletions packages/redux-toolkit-floating-promises/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"noEmit": true,
"strict": true
},
}