Created
August 6, 2019 07:51
-
-
Save iwillwen/79978ff4133b05e4460aa46a36ba9636 to your computer and use it in GitHub Desktop.
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
import * as Vuex from 'vuex' | |
type GettersOption<S> = { [key: string]: (state: S) => unknown } | |
type Store<S, M, A, G extends GettersOption<S>> = { | |
state: S | |
mutations: M | |
actions: A | |
getters: { [key in keyof G]: ReturnType<G[key]> } | |
} | |
class VuexStore<S, M, A, G extends GettersOption<S>> { | |
option = null | |
constructor(options: any = {}) { | |
this.option = options | |
} | |
state<O>(state: O) { | |
return new VuexStore<O, M, A, GettersOption<O>>(Object.assign(this.option, { state })) | |
} | |
getters<O extends { [key: string]: (state: S) => any }>(getters: O) { | |
return new VuexStore<S, M, A, O>(Object.assign(this.option, { getters })) | |
} | |
create(): Store<S, M, A, G> { | |
return new Vuex.Store(this.option) as any | |
} | |
} | |
const store = (new VuexStore) | |
.state({ | |
count: 0 | |
}) | |
.getters({ | |
plus: state => state.count + 1, | |
string: state => state.count.toString() | |
}) | |
.create() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment