Skip to content

Instantly share code, notes, and snippets.

@aprilrd
Created May 8, 2018 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aprilrd/92b226dce5712ffc74b2945a0324a633 to your computer and use it in GitHub Desktop.
Save aprilrd/92b226dce5712ffc74b2945a0324a633 to your computer and use it in GitHub Desktop.
Typescript + Redux + Typescript Readonly Interfaces
// scaffolding
interface IPostState
extends Readonly<{
post: IPost | null;
isLoading: boolean;
}> {} // this has to be a Readonly interface as well.
// reducers
const INITIAL_STATE: IPostState = {
post: null,
isLoading: false,
};
function postReducer(state: IPostState = INITIAL_STATE, action): IPostState {
switch (action.type) {
case "FETCHED": {
return {
post: action.payload.post,
isLoading: false,
};
}
case "UPDATED_TITLE": {
return {
...state,
post: {
...state.post,
title: action.payload.title,
},
};
}
default: {
return state;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment