question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

actionSanitizer & stateSanitizer with TypeScript

See original GitHub issue

Hi !

I’d like to use these EnhancerOptions with TypeScript and my typed state but can’t find a way to set them correctly. I don’t want to use any and to lose types.

Here’s a small reproductible example (type StateSanitizer comes from redux-devtools) :

type AppState = {
    msg: string;
    secret: string;
}
type StateSanitizer = <S>(state: S) => S;

const state: AppState = { msg: "Hello", secret: "SECRET !" };
const sanitizer: StateSanitizer = (state: AppState) => {
    const result = { ...state };
    result.secret = '';
    return result; // Casting to AppState doesn't help
};

Which result in a compiler error :

Type '(state: AppState) => { msg: string; secret: string; }' is not assignable to type 'StateSanitizer'.
  Types of parameters 'state' and 'state' are incompatible.
    Type 'S' is not assignable to type 'AppState'.

I tried this too (same error) :

const sanitizer: StateSanitizer = <S extends AppState>(state: S) => {
    const result = { ...state };
    result.secret = '';
    return result as S;
};

I’m probably missing something but can’t find what.

Here’s my application store creation file :

import { applyMiddleware, createStore } from "redux";
import { composeWithDevTools, EnhancerOptions } from "redux-devtools-extension";
import thunk from "redux-thunk";
import rootReducer from "./reducers";
import { AppState } from "./state";

const stateSanitizer = (state: AppState) => state;
const options: EnhancerOptions = { stateSanitizer };
const composeEnhancers = composeWithDevTools(options);

const store = createStore(
  rootReducer,
  composeEnhancers(applyMiddleware(thunk))
);

export default store;

Which fails with the same issue at compile-time.

Thanks for you help and suggestions 😉

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:11
  • Comments:5

github_iconTop GitHub Comments

2reactions
pkuczynskicommented, Feb 16, 2021

Something like this should work:

const composeEnhancers = composeWithDevTools({
    stateSanitizer: <S extends Partial<AppState>>(state: S): S => {
        const { user, ...rest } = state

        return {
            ...rest
        } as S
    }
})
1reaction
isaaclymancommented, May 11, 2022

Update: I’ve got it working with the following:

actionSanitizer: <S extends Action<string>>(action: S & {payload: any}): S => {
  if (action.type === 'LOAD_DATA') {
    action.payload; // This works
  }
  return action;
}

In my case I’m using flux-standard-action so actions have a payload attached.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use the redux-devtools-extension/logOnlyInProduction ...
To help you get started, we've selected a few redux-devtools-extension/logOnlyInProduction.composeWithDevTools examples, based on popular ways it is used in ...
Read more >
Redux devtool extenstion : How to Add Features property to ...
I'm using RTK here so the use of composeWithDevTools is not needed as devTools option handles that, but I'm assuming if you doing...
Read more >
Use sanitizers to avoid Redux Devtools crash
Use actionSanitizer and stateSanitizer to avoid Redux Devtools crash because of excessive use of memory. Tagged with react, redux, devtools, ...
Read more >
teadux - npm
TypeScript icon, indicating that this package has built-in type declarations ... Action, Deps>(deps) const {actionSanitizer, stateSanitizer} ...
Read more >
redux-devtools-extension JavaScript and Node.js ... - Tabnine
... initialState, devToolsEnhancer( // options like actionSanitizer, stateSanitizer )); }. origin: andrewangelle/typescript-react-redux-example ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found