Reduce Redux's action-creator boilerplates without library support in TypeScript

github logo ・1 min read

// Define action-creators
export const fetchPostsRequest = () => ({
  type: "FETCH_POSTS" as const
});
export const fetchPostsSuccess = (posts: any) => ({
  type: "FETCH_POSTS_SUCCESS" as const,
  payload: { posts }
});
export const fetchPostsFailure = (error: Error) => ({
  type: "FETCH_POSTS_FAILURE" as const,
  error
});

// Define Action and ActionTypes
export type FetchPostAction = ReturnType<
  | typeof fetchPostsRequest
  | typeof fetchPostsSuccess
  | typeof fetchPostsFailure
>
export type FetchPostsActionTypes = FetchPostAction["type"];

// Let's use
const action: FetchPostAction = {
  type: "FETCH_POSTS_FAILURE"
}; // -> Property 'error' is missing
twitter logo DISCUSS
Classic DEV Post from Jan 28

a11y and JS - A Seemingly Unconventional Romance

I want you to know that JavaScript isn’t the enemy of accessibility. Instead, lack of empathy is.

Ryoji Ishii profile image
JavaScript / TypeScript / Scala / Python / Rust
Join dev.to

Be a better developer. Free forever.