Skip to content
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

Add TypeScript definitions #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,22 @@ let store = createInjectStore(
{ combineReducers }
);
```

## TypeScript
React Reducer Injector provides TypeScript definitions.
Some of the types require `typescript@^4.1` in order to work properly.

```typescript
const rootReducerObject = { foo: { bar: (state: number, _action) => state } }
const store = createInjectStore(rootReducerObject /* ... */)
type AppState = StateFromDeepReducersMapObject<typeof rootReducerObject>

// ... dynamically loaded module
const moduleReducer = (state: string, _action) => state
injectReducer('baz.quz', moduleReducer)
type ModuleState = StateShapeFromReducerPath<'baz.quz', typeof moduleReducer>

// ... with its selectors that are capable of accessing available state
type AvailableState = AppState & ModuleState
const getBarQuz = (state: AvailableState) => `${state.foo.bar} ${state.baz.quz}`
```
97 changes: 97 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import type {
AnyAction,
Reducer,
StoreEnhancer,
PreloadedState,
Store as ReduxStore,
} from 'redux'

export type ReducerDeepMapObject<S, A extends AnyAction> = {
[key: string]: ReducerDeepMapObject<S, A> | Reducer<S, A>
}

/**
* Get State type computed from the reducer object that is used in e.g. in `createInjectStore`
*/
export type StateFromDeepReducersMapObject<M extends ReducerDeepMapObject<any, any>> = {
[P in keyof M]: M[P] extends Reducer<infer S, any>
? S
: M[P] extends ReducerDeepMapObject<any, any> ? StateFromDeepReducersMapObject<M[P]> : never
}

type Store<S, A extends AnyAction> = ReduxStore<S, A> & {
injectedReducers: ReducerDeepMapObject<S, A>
}

/**
* Compute State Shape based on string path
*
* @example
* StateShapeFromPath<'foo.bar', number>
* // returns type {foo: {bar: number}}
*/
export type StateShapeFromPath<
PATH extends string,
T
> = PATH extends `${infer PATH_LEFT_PART}.${infer PATH_RIGHT_PART}`
? StateShapeFromPath<PATH_LEFT_PART, StateShapeFromPath<PATH_RIGHT_PART, T>>
: { [key in PATH]: T }

/**
* Compute State Shape based on string path and
* the Reducer that is injected on that path
*/
export type StateShapeFromReducerPath<
PATH extends string,
T extends Reducer
> = StateShapeFromPath<PATH, ReturnType<T>>

/**
* A store creator is a function that creates a Redux store. Like with
* dispatching function, we must distinguish the base store creator,
* `createStore(reducer, preloadedState)` exported from the Redux package, from
* store creators that are returned from the store enhancers.
*
* @template S The type of state to be held by the store.
* @template A The type of actions which may be dispatched.
* @template Ext Store extension that is mixed in to the Store type.
* @template StateExt State extension that is mixed into the state type.
*/
export interface StoreCreator {
<S, A extends AnyAction, Ext, StateExt>(
reducer: ReducerDeepMapObject<S, A>,
enhancer?: StoreEnhancer<Ext, StateExt>
): Store<S & StateExt, A> & Ext
<S, A extends AnyAction, Ext, StateExt>(
reducer: ReducerDeepMapObject<S, A>,
preloadedState?: PreloadedState<S>,
enhancer?: StoreEnhancer<Ext, StateExt>
): Store<S & StateExt, A> & Ext
}

export const createInjectStore: StoreCreator
export const injectReducer: (
key: string,
reducer: Reducer<any, any> | ReducerDeepMapObject<any, any>,
force?: boolean,
store?: Store<any, any>
) => void

export const combineReducersRecurse: <S = any, A extends AnyAction = any>(
reducers: Reducer<S, A> | ReducerDeepMapObject<S, A>
) => Reducer<S, A>

export const reloadReducer: (
key: string,
reducer: Reducer<any, any> | ReducerDeepMapObject<any, any>,
store?: Store<any, any>
) => void

export const injectReducerBulk: (
reducer: {
key: string
reducer: Reducer<any, any> | ReducerDeepMapObject<any, any>
}[],
force?: boolean,
store?: Store<any, any>
) => void