Git Product home page Git Product logo

Comments (6)

the-dr-lazy avatar the-dr-lazy commented on May 28, 2024 2

The return type of a reducer should be what it actually returns and it is equal to return type of all handlers it has. So the return type of a reducer can be inferred from the return type of all handlers it has.

Also, because of the reactivity nature of the state in Flux, the return type of a reducer (which in the following called output/next state) should extend the input/prev state type. For now, the input/prev state type is immutable but it is going to also be mutable in #58 and according to the previous sentence the output/next state can be as follow:

  1. immutable or mutable when the input/prev state is immutable.
type NamesState = DeepImmutableArray<string>

const defaultNamesState: NamesState = Object.freeze([])

const namesReducer = createReducer(defaultNamesState, handleAction => [
  handleAction(actionA, (state, { payload }) => [...state, payload]), // string[]
  handleAction(actionB, (state) => state) // DeepImmutableArray<string>
])

namesReducer(...) // string[] | DeepImmutableArray<string>
  1. just mutable when the input/prev state is mutable.
type NamesState = string[]

const defaultNamesState: NamesState = []

const namesReducer = createReducer(defaultNamesState, handleAction => [
  handleAction(actionA, (state, { payload }) => [...state, payload]), // string[]
  handleAction(actionB, (state) => Object.freeze(state)) /* throws error
                                                            because of that input/prev state 
                                                            defined as a mutable array
                                                            but the output/next state is a `ReadonlyArray` */
])

namesReducer(...) // string[] 

@rlesniak @Haaxor1689 @LouizFC and other guys who interested in Deox, It would be great to have your opinion/feedback about the above solution because without that the growth of the Deox is not possible. If you are agree hit 👍; if not hit 👎 (Also it is awesome to leave a comment to describe your opinion/solution).

from deox.

the-dr-lazy avatar the-dr-lazy commented on May 28, 2024

Hi @rlesniak
Before any release for this issue as a hacky solution the best way I can imagine is something like below:

type InferState<T> = {
  [Key in keyof T]: T[Key] extends DeepImmutable<infer A> ? A : T[Key]
};

export type RootState = InferState<ReturnType<typeof rootReducer>>;

The above solution doesn't solve the actual problem. Can you figure out what should be the return type of the reducer that has been created by createReducer?

from deox.

rlesniak avatar rlesniak commented on May 28, 2024

Thanks.
So if I understand your question correctly, in my case it should be just:

type RootState = {
    status: Readonly<{
        error: string;
        details: StatusInfo;
        fetching: boolean;
    }>;
}

so only without DeepImmutableObject.

from deox.

rlesniak avatar rlesniak commented on May 28, 2024

Are you sure its fixed? TSC still throws DeepImmutable error, check this sandbox mentioned in first post

from deox.

the-dr-lazy avatar the-dr-lazy commented on May 28, 2024

You are right and that is due to obligatory behavior of Deox in prev (input) state type which is wrapped by DeepImmutable (discussed in #58) and can't be fixed without breaking change. So if you agree, let's follow it in its specific place.

from deox.

the-dr-lazy avatar the-dr-lazy commented on May 28, 2024

Also, I found that combineReducer type signature in Redux isn't so accurate. I will try to send a PR to reduxjs/redux for fixing that.

from deox.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.