-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
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
Support for typed Actions #355
Comments
Yeah, record/replay and associated features won't work with an action class. Does TypeScript support discriminated unions or something? |
Related: flux standard action |
TypeScript has discriminated unions, so when I get some free time I will try to figure something out with it. @cesarandreu is this |
Ok, I figured it out, I create plain object and cast it as my desired class, thanks to this I can get syntax checking with plain objects: // PhotoActions.ts
export function processPhoto(photoData:PhotoData)
{
return {
type: PROCESS_PHOTO,
data: photoData
} as Action;
} |
It's just a recommendation. You can follow it or ignore it. Closing as the initial issue appears to be solved by casting. |
I am also using Redux with Typescript and trying to get some static typing guarantees on my actions. What I arrived at was something that looked like this: ActionTypes.ts
Actions.ts
UserManagementReducer.ts
The only problem is that I get the error I would really like to be able to leverage the advantages of both Typescript and Redux. Any idea what is going on here? Is the 'plain object' check perhaps just too strict? |
Can you try using discriminated unions instead of classes? |
As far as I know Typescript does not support discriminated unions. It supports union types, like these: http://blogs.msdn.com/b/typescript/archive/2014/11/18/what-s-new-in-the-typescript-type-system.aspx?PageIndex=3 As far as 'the wrong way to achieve type safety' goes - if discriminated unions were available I would use them! But we work with what we have, and this approach does give me type safety and compile-time checks. |
(realized I left it out of my initial post) |
See a workaround for TypeScript 1.6: #992 (comment) |
There is another approach for action typing: #992 (comment) |
I am sorry, action can only be plain objects. So this is not the solution. @aikoven 's solution seem's great! |
In fact, it works with "Redux DevTools" chrome extension, while it fails with subj exception in "clean" chrome.
The restriction encouraged me to create a hack around it ++ ok, i have read the other thread, and then i read the thread with a guy who wants to put functions into action, and i see that this is exactly the thing you did not want people to do. About the "legitimate" request to relax a constraint - between having an interface plus function to create a plain object and having a class the difference is that there are considerably less code for class. export interface IMyAction extends IAction
{
content: string;
}
export const createMyAction = (content: string) => {
return {
type: "MY_ACTION",
content: content
}
} vs export class MyAction implements IAction {
public type: string = "MY_ACTION";
constructor(public content: string ){}
} On the receiving end, i.e. inside the reducer, there is no difference between casting to interface or to class both are just compile time operations. |
I often do it like this -> http://stackoverflow.com/questions/35482241/how-to-type-redux-actions-and-redux-reducers-in-typescript/41442542#41442542 I have an
I have a
I have an action type constant:
And I have an interface for the action (check out the cool use of
I have an action creator function:
Now my reducer looks something like this:
And I have a reducer function per action:
|
I'm using Redux alongside with TypeScript and I created my custom class
Action
andActionCreator
When I try to dispatch this action I get error
Actions must be plain objects. Use custom middleware for async actions.
Is it really necessary for action objects to be plain objects?The text was updated successfully, but these errors were encountered: