Skip to content

Type defineStore for TypeScripte? #1462

Discussion options

You must be logged in to vote

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:

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 s…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@rmxxtt
Comment options

Answer selected by rmxxtt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants