Git Product home page Git Product logo

Comments (19)

LouizFC avatar LouizFC commented on May 28, 2024 1

I think I found the cuprit:

// createReducer
export declare function createReducer<State, HM extends HandlerMap<State, any>>(
  defaultState: State,
  handlerMapsCreator: (handle: CreateHandlerMap<State>) => HM[]
): (
  state: DeepImmutable<State> | undefined,
  action: HM extends HandlerMap<State, infer T> ? T : never
) => State | DeepImmutable<State>;

//redux
export type Reducer<S = any, A extends Action = AnyAction> = (
  state: S | undefined,
  action: A
) => S
// deox/dist/types/Reducer
export declare type Reducer<State, Actions> = (
  prevState: DeepImmutable<State>, // < needs to be DeepImmutable<State> | undefined
 action: Actions
) => DeepImmutable<State> | State;

from deox.

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

If we were to make the return type of createReducer explicit using the DeoxReducer above, how would be it declared?

It should be declared like how Action type was declared. By inferring the return type of HandlerMap(s).

type HandlerMap<TPrevState, TAction, TNextState extends TPrevState> = {
  [type in TAction['type']]: Handler<TPrevState, TAction, TNextState>
}

export type InferActionFromHandlerMap<
  THandlerMap extends HandlerMap<any, any, any>
> = THandlerMap extends HandlerMap<any, infer T, any> ? T : never

export type InferNextStateFromHandleMap<
  THandlerMap extends HandlerMap<any, any, any>
> = THandlerMap extends HandlerMap<any, any, infer T> ? T : never

function createReducer<
  TPrevState,
  THandlerMap extends HandlerMap<TPRevState, any, any>
>(
  defaultState: TPrevState,
  handlerMapsCreator: (handle: CreateHandlerMap<TPrevState>) => THandlerMap[]
) {
  // ...

 return (
    state = defaultState,
    action: InferActionFromHandlerMap<THandlerMap>
  ): InferNextStateFromHandleMap<THandlerMap> => {
    // ...
  }
}

I hope I understood your question correctly. If not, excuse me for my bad English and please ask your question with more details.

from deox.

LouizFC avatar LouizFC commented on May 28, 2024 1

That is exactly what I was looking for. I am not a "typescript expert" so I have not yet understood the infer keyword completely.

Your example made it clear in my mind, Thank you very much

from deox.

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

Sorry, I don't understand the topic. Can you explain more? or maybe reproduce it with codesandbox or whatever.

UPDATE: Whooops, the main comment was not loaded for me at that moment :|

from deox.

LouizFC avatar LouizFC commented on May 28, 2024

@thebrodmann Sorry, I have pressed Enter by accident when typing the title

from deox.

LouizFC avatar LouizFC commented on May 28, 2024

For completeness, here is the ParamTableActionCreators

export function createParamActionCreators(paramTableName: string) {
  return {
    addParam: createActionCreator(
      `[${paramTableName}] ADD_PARAM`,
      (resolve) => (param: Param) => resolve(param)
    ),
    removeParam: createActionCreator(
      `[${paramTableName}] REMOVE_PARAM`,
      (resolve) => (index: number, param: Param) => resolve({ index, param })
    ),
    updateParam: createActionCreator(
      `[${paramTableName}] UPDATE_PARAM`,
      (resolve) => (index: number, param: Param) => resolve({ index, param })
    ),
    reorderParams: createActionCreator(
      `[${paramTableName}] REORDER_PARAMS`,
      (resolve) => (fromIndex: number, toIndex: number) =>
        resolve({ fromIndex, toIndex })
    ),
  };
}

type ParamTableActionCreators = ReturnType<typeof createParamActionCreators>;

from deox.

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

Reducer<DeepImmutableArray | undefined, ...> is not assignable to Reducer<DeepImmutableArray, ...>

I think the problem is the difference in types between acceptable reducer of the target method and which you are giving to it. I don't think it is because of not using type helper in createReducer return type.

from deox.

LouizFC avatar LouizFC commented on May 28, 2024

I understand. But what I mean is: The type given by deox/dist/types/Reducer is not compatible with createReducer return type.

I have made no further transformations as you can see, yet I am having a compile time error at return normalizeReducer(deoxReducer);.

I will make some tests to try to prove my argument, maybe I am overthinking something.

from deox.

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

You're right.
Actually, Deox's Reducer type was used in handleAction which never call with an undefined state.

from deox.

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

Have you any suggestion to handle this situation?

from deox.

LouizFC avatar LouizFC commented on May 28, 2024

On the top of my mind, you could do this:

export declare type CreateHandlerMap<State> = <
  AC extends ActionCreator<string>,
  Actions extends AnyAction = AC extends (...args: any[]) => infer T ? T : never
>(
  actionCreators: AC | AC[],
  handler: HandlerReducer<State, Actions> // < change here
) => HandlerMap<State, Actions>;

And on deox/types:

export declare type HandlerReducer<State, Actions> = (
  prevState: DeepImmutable<State>,
 action: Actions
) => DeepImmutable<State> | State;
/* maintain the old Reducer export for backward compatibility maybe? 
 * if that is not a concern, DeoxReducer could be renamed to just Reducer */
export declare type Reducer<State, Actions> = HandlerReducer<State, Actions> 

export declare type DeoxReducer<State, Actions> = (
  prevState: DeepImmutable<State> | undefined,
 action: Actions
) => DeepImmutable<State> | State;

from deox.

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

I agree. Handler and Reducer types should be separate as Handler always gives non- undefined state but Reducer can give undefined state.

I don't think there is a need for backward compatibility, because Reducer type does not present in public API.

from deox.

LouizFC avatar LouizFC commented on May 28, 2024

I have a question that is kinda offtopic but related to this.

If we were to make the return type of createReducer explicit using the DeoxReducer above, how would be it declared?

In DeoxReducer<State, Action> The State type is obvious, but I am having some trouble wrapping my head around the Action type, because it extracts the action of each of the HandlerMaps

How can I express this type in the return type?

from deox.

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

@LouizFC Are you interested to work on this issue?

from deox.

LouizFC avatar LouizFC commented on May 28, 2024

@thebrodmann I could work in this issue this weekend, but I am not sure exactly what should I do.

Renaming the current Reducer to HandlerReducer and adding a new Reducer with the undefined fix would be sufficient? I could also try to make the return type of createReducer explicit

What do you think should be done?

from deox.

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

Renaming the current Reducer to HandlerReducer and adding a new Reducer with the undefined fix would be sufficient?

Yes. I suggest the name Handler instead of HandlerReducer. Also, HandlerMap, CreateHandlerMap and createHandlerMap uses the current Reducer type (future Handler). So those should use Handler instead of Reducer.

I could also try to make the return type of createReducer explicit.

I will do it on #55.

Also, I suggest you fork a branch from the next branch and work on it due to the huge refactor in b350de4.

So, I will assign this issue to you. Thank you.

from deox.

LouizFC avatar LouizFC commented on May 28, 2024

@thebrodmann I am having a problem while pushing the changes.

Test Suites: 6 passed, 6 total
Tests:       202 passed, 202 total
Snapshots:   6 obsolete, 6 written, 95 passed, 101 total
Time:        27.952s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test-dts: `jest -c dts-jest.config.js "--bail"`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] test-dts script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\User\AppData\Roaming\npm-cache\_logs\2019-06-04T19_47_36_896Z-debug.log
husky > pre-push hook failed (add --no-verify to bypass)
error: failed to push some refs to 'https://github.com/LouizFC/deox.git'

Edit: I tried using the "--updateSnapshot" flag, but the problem persists

from deox.

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

Let's try git push with --no-verify flag to skip the hook.

from deox.

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

This issue has been solved. Thanks to @LouizFC.

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.