Last active
November 15, 2019 21:31
-
-
Save lafiosca/a992fa57dc2bb871108fbfc1ef1ad93e to your computer and use it in GitHub Desktop.
redux-persist TypeScript migrations example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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