-
-
Notifications
You must be signed in to change notification settings - Fork 99
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
v5.0.0 - Rewrite and simplify createAction
API
#143
Comments
createAction
API using generic tuple typescreateAction
API using generic tuple types
createAction
API using generic tuple typescreateAction
API
@IssueHunt has funded $150.00 to this issue.
|
This is not related to #116, right? |
@sjsyrek It means it would become unnecessary It's still just a proposal, It's not finalized yet, open to discussion. |
I updated the proposal with improved |
This looks great! Edit - is there a way to set the |
Hey @davidgovea, please help me to figure out this feature. I need to understand your use-case and how you would like to use this feature from a user perspective. Thanks |
Would something like this work with the new API?
If not I guess I could simply pass identity function with the type in 1st and 3rd param, but something like this would also be pretty useful. |
Hey @paolostyle, that's an interesting idea, I think that might be possible. I haven't thought of it and I really appreciate your feedback. I'll be working on the implementation this weekend so I'll give it a shot. If that would work I think I would drop the second variant and use only the version from your proposal as it's basically solving both use-cases with one function which is a superior API. Challenge 1:One limitation is it'll only allow mapper functions with a single argument. I think it might still be ok because the previous version of async action has the same limitation and practically no one was complaining about it, so I guess most users can live without it. Possible solution: const action = createAsyncAction(
'REQ_TYPE',
['SUCCESS_TYPE', (res, meta) => {
// do something with res
}],
'FAILURE_TYPE'
)<[Request, Meta], (res: Response, meta: Meta) => Results, [Error, Meta]>; |
will v5 release fix
|
@bboydflo yes you are correct, createAction is currently not supporting optional payload type correctly, it'll be fixed in new API. |
Hi @piotrwitek - These changes look great. I wanted to quickly check-in and see how progress was coming. Thank you for all the hard work :) |
Just returned from a Summer Break, will come back to this real soon! |
Not sure if this is isolated to 5.0.0-3 or not (if you suspect it's not just let me know and I'll make a new issue), but Example (the
|
@jstheoriginal It's a good find, thanks for reporting. |
@jstheoriginal Not specific to 5.0. I remember this coming up when I attempted to use |
Hey, thanks for the great library! It makes working with using react-redux with TS a lot better. I was wondering if you'd seen the TypeScript action creator code in NgRx, the Angular redux library? It does a good job of avoiding boilerplate, while retaining type safety for things like reducers. Might be a source of inspiration. |
@jonrimmer has funded $50.00 to this issue.
|
Hey @jonrimmer, |
No problem! The actual creators are fairly simple, they work like your examples above. The main thing I noticed is that they've managed to get type inference on the reducer functions without having to register root actions or extend the module, e.g. const scoreboardReducer = createReducer(
initialState,
on(loadTodos, state => ({ ...state, todos })),
on(ScoreboardPageActions.awayScore, state => ({ ...state, away: state.away + 1 })),
on(ScoreboardPageActions.resetScore, state => ({ home: 0, away: 0 })),
on(ScoreboardPageActions.setScores, (state, { game }) => ({ home: game.home, away: game.away }))
); So, similar to |
@jonrimmer actually that automatic inference is easy to add, what differentiate typesafe-actions from all other libraries is the fact that |
|
For anyone coming from search engines with the following bug:
My mistake was that I didn't see the docs were not updated yet. I solved it by using the new createAction() API given by @piotrwitek in the first post of this thread. Example:
Go and checkout the other breaking changes https://github.com/piotrwitek/typesafe-actions/releases which are not in the official docs yet, too. |
Maybe this should be a new issue, but I'm not sure I understand the benefit/point of the extra function call in export const updateProviders = createAction(
"UPDATE_PROVIDERS",
(provider: Provider) => provider,
)<Provider>(); The generic |
@mAAdhaTTah good question. export const updateProviders = createAction(
"UPDATE_PROVIDERS",
)<Provider>(); But it can be useful in other more complex cases, especially when you want to set the constraints for the types you expect. Consider this: const deleteUser = createAction(
'TYPE1',
(user: User) => user.id,
)<string>();
deleteUser(userObject) // { type: 'TYPE1', payload: string } This will create an action creator that will accept argument user of type User, but the payload will be string. |
Is there a possibility to add meta data to all the actions?
I can call I'd need to have a certain metadata field on ALL the actions for my custom redux middleware. |
@DalderupMaurice yes you can just edit the types arguments: const actionWithMeta = createAsyncAction(
'REQ_TYPE', 'SUCCESS_TYPE', 'FAILURE_TYPE', 'CANCEL_TYPE',
)<[Request, Meta], [Response, Meta], [Error, Meta], [undefined, Meta]>(); |
Hello! I'm trying to configure a basic action/reduce setup. I've got the following action: export const editorMetaAuthorChange = createAction('editor/META_AUTHOR_CHANGE')<string>() Then this should be the relative reducer: export interface EditorState {
meta: {
author: string
name: string
description: string
tags: string[]
}
}
const initialState: EditorState = {
meta: {
author: '',
name: '',
description: '',
tags: [],
},
}
const reducer = createReducer(initialState)
.handleAction(editorMetaAuthorChange, (state, { payload }) => ({
...state,
meta: {
...state.meta,
author: payload,
},
})) The problem is that in Thanks :) |
Im having same issue // actions
export const updatePreferenceRequest = createAction(
'@preference/UPDATE_PREFERENCE_REQUEST'
)<UpdatePreferenceData>();
// REDUCER
const INITIAL_STATE: StatePreference = {
totalCountMax: 100,
vibration: true,
sound: true,
loading: false,
};
const preferenceReducer = createReducer(INITIAL_STATE)
.handleAction(
updatePreferenceRequest,
(state: StatePreference, { payload }) => ({
...state,
loading: true,
}); state and payload |
@allandiego I've kind of "resolved" writing a supporting library: #229 |
@elegos i ended up switching to the official redux lib: |
Are generics not supported for enum Element {
Elem1 = 'elem1',
Elem2 = 'elem2',
}
type ElementValueMapper = {
[Element.Elem1]: string;
[Element.Elem2]: number;
};
type Explicit = ElementValueMapper[Element.Elem1]; // correct type string
const action = createCustomAction(
'someAction',
<T extends Element>(e: T, value: ElementValueMapper[T]) => ({
payload: { e, value },
})
);
action(Element.Elem1, 5); // type of value is string | number, should be only string It is possible when I create action by hand: const action = <T extends Element>(
name: T,
value: ElementValueMapper[T]
): PayloadAction<'changevalue', { name: T; value: ElementValueMapper[T] }> =>
({
type: 'changevalue',
payload: { name, value },
} as const);
action(Element.Elem1, 5); // Argument of type '5' is not assignable to parameter of type 'string'. |
Is your feature request related to a real problem or use-case?
Currently,
createAction
API is not optimal and require some unnecessary boilerplate code (historically it was created pre TS v3.0 so there was a lot of limitations having an impact on API shape):Describe a solution including usage in code example
Today with recent TypeScript features I can finally rewrite the API to something simpler, more intuitive and more consistent across the board.
This will resolve few existing feature requests:
createAction
createAsyncAction
createCustomAction
All remaining non-standard use-cases you will be able to cover with
createCustomAction
Who does this impact? Who is this for?
All TypeScript users
Additional context (optional)
createAction
andcreateStandardAction
will be deprecated because newcreateAction
will be able to replace their function.In v5, the old API will be kept in
deprecated
import so incremental migration is possible:IssueHunt Summary
Backers (Total: $200.00)
Become a backer now!
Or submit a pull request to get the deposits!
Tips
IssueHunt has been backed by the following sponsors. Become a sponsor
The text was updated successfully, but these errors were encountered: