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 "ReducerState" type to definition file #3419

Closed
septs opened this issue May 2, 2019 · 2 comments
Closed

Add "ReducerState" type to definition file #3419

septs opened this issue May 2, 2019 · 2 comments

Comments

@septs
Copy link

septs commented May 2, 2019

import { combineReducers } from "redux";

import global, { GlobalState } from "./global";
import foo, { FooState } from "./global";
import bar, { BarState } from "./global";

export default combineReducers({
    global,
    foo,
    bar
});

export interface RootState {
    global: GlobalState,
    foo: FooState,
    bar: BarState,
}
import { combineReducers, Reducer } from "redux";

declare type ReducerState<T> = T extends Reducer<infer S> ? S : never;

export const reducer = combineReducers({
    global,
    foo,
    bar,
});

// get global internal state typing
export type GlobalState = ReducerState<typeof global>;
// get root state with combine all state typing
export type RootState = ReducerState<typeof reducer>;

This can greatly reduce the writing of boilerplate code.

RootState can be automatically generated.


infer keyword release for TypeScript 2.8

@timdorr
Copy link
Member

timdorr commented May 2, 2019

This was already added via #3411. It hasn't been released yet.

@timdorr timdorr closed this as completed May 2, 2019
@septs

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants