> For the complete documentation index, see [llms.txt](https://redux-undo.js.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://redux-undo.js.org/main/working-with-ts.md).

# Working with Typescript

## Importing redux-undo in typescript

By default, you must use the `import = require` syntax when using typescript. You can read more in their [documentation](https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require).

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

```javascript
{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  }
}
```

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

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

```typescript
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 [initialState with redux-undo](/main/upgrading-to-1.0.md#initialstate).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://redux-undo.js.org/main/working-with-ts.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
