redux-undo
  • About
  • Main
    • FAQ
    • Examples
      • Undoing/redoing batches of actions at a time
      • Converting a regular reducer to undoable reducer
    • Upgrading to 1.0
    • Working with Typescript
Powered by GitBook
On this page
  • Importing redux-undo in typescript
  • Typing Initial State

Was this helpful?

  1. Main

Working with Typescript

PreviousUpgrading to 1.0

Last updated 5 years ago

Was this helpful?

Importing redux-undo in typescript

By default, you must use the import = require syntax when using typescript. You can read more in their .

import ReduxUndo = require('redux-undo')

const undoable = ReduxUndo.default

Alternatively, by changing your tsconfig.json, you can import using the ES style syntax. Both esModuleInterop and allowSyntheticDefaultImports need to be set to true.

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  }
}
// myReducer.ts
import undoable, { ActionCreators } from 'redux-undo'

Typing Initial State

While using typescript, you might run into issues when providing initial state for your undoable reducers. To get around this issue, you will need to cast types, a.k.a. blantly lie to the compiler.

When you are providing state as your reducer will receive them without a predefined history, you must type cast to any then StateWithHistory.

import { StateWithHistory } from 'redux-undo'

createStore(rootReducer, {
  // Have to cast to any first
  undoableState: (initialState as any) as StateWithHistory<typeof initialState>
})

With a predefined history, you can lie "more directly."

createStore(rootReducer, {
  undoableState: withHistory as StateWithHistory<typeof withHistory['present']>
})

Remember, you have to pass all of and only the fields past, present, and future in the history. Read more about .

documentation
initialState with redux-undo