Comments (19)
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.
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.
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.
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.
@thebrodmann Sorry, I have pressed Enter by accident when typing the title
from deox.
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.
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.
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.
You're right.
Actually, Deox's Reducer
type was used in handleAction
which never call with an undefined
state.
from deox.
Have you any suggestion to handle this situation?
from deox.
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.
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.
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 HandlerMap
s
How can I express this type in the return type?
from deox.
@LouizFC Are you interested to work on this issue?
from deox.
@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.
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.
@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.
Let's try git push
with --no-verify
flag to skip the hook.
from deox.
This issue has been solved. Thanks to @LouizFC.
from deox.
Related Issues (20)
- handleAction allows you to return a state object with any additional properties HOT 3
- 2.1.2 breaking change on ActionCreator type HOT 4
- The automated release is failing 🚨
- The automated release is failing 🚨 HOT 1
- ofType does no longer automatically infer action type from general source in pipe HOT 6
- createAction with only a type parameter results in function, which allows any arguments HOT 5
- Failed to resolve 'rxjs/operators' HOT 2
- Explain or remove `resolve => x => resolve(x)` pattern HOT 1
- Create polymorphic (generic) action creator HOT 2
- ESLint explicit-function-return-type rule error HOT 4
- Bundle size notes HOT 5
- Add "default" section in createReducer() HOT 4
- The automated release is failing 🚨
- Why was Deox deprecated? HOT 1
- The automated release is failing 🚨 HOT 1
- Prototype pollution in immer dependency HOT 2
- The automated release is failing 🚨
- Add Payload type utility HOT 1
- Is there a way to create request action generator? HOT 7
- Looking for a maintainer...
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deox.