Type defineStore for TypeScripte? #1462
Answered
by
posva
rmxxtt
asked this question in
Help and Questions
-
i'm trying to make a store factory, but i don't know what type of object to specify, i can't find the answer anywhere import { defineStore } from "pinia";
const storeList = {}
export function storeFactory (name: string) {
if (name in list){
return list[name]
} else {
list[name] = defineStore(name, {
state: () => {
return {
count: 0,
}
},
getters: {
doubleCount: (state) => state.count + state.count
}
})
return list[name]
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
posva
Jul 21, 2022
Replies: 1 comment 1 reply
-
You probably don't want to do this because you would lose all types. You have a To keep all types you need to reorganize the code so the type inference can be automatic: function defineStoreWithName(name: string) {
return defineStore(name, {
// ...
})
}
const storeMap = new Map<string, ReturnType<typeof defineStoreWithName>>()
export funciton storeFactory(name: string) {
if (!storeMap.has(name)) {
storeMap.set(name, defineStoreWithName(name))
}
return storeMap.get(name)!
} Most of the time this means you should have some collection within the store instead. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
rmxxtt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You probably don't want to do this because you would lose all types. You have a
StoreDefinition
type that is used to define stores and can be manually used but I will advise you against it in this scenario.To keep all types you need to reorganize the code so the type inference can be automatic:
Most of the time this means you should have some collection within the s…