Skip to content

Instantly share code, notes, and snippets.

@lafiosca
Last active November 15, 2019 21:31
Show Gist options
  • Save lafiosca/a992fa57dc2bb871108fbfc1ef1ad93e to your computer and use it in GitHub Desktop.
Save lafiosca/a992fa57dc2bb871108fbfc1ef1ad93e to your computer and use it in GitHub Desktop.
redux-persist TypeScript migrations example
interface RootStateV0 {
a: {
b: number;
};
d: {
e: number;
};
}
interface RootStateV1 {
a: {
b: number;
c: string;
};
d: {
e: string;
};
}
interface RootStateV2 {
a: {
c: string;
};
d: {
e: string;
};
f: string;
}
const migrations = {
1: (state: RootStateV0): RootStateV1 => ({
...state,
a: {
...state.a,
c: 'default', // add string field
},
d: {
e: `${state.d.e}`, // convert number field to string field
},
}),
2: (state: RootStateV1): RootStateV2 => {
const { b, ...aWithoutB } = state.a;
return {
...state,
a: aWithoutB, // remove number field
f: 'default', // add string field,
};
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment