Skip to content

What's the best way to tell TS that getters are writable in unit tests? #2649

Discussion options

You must be logged in to vote

This is the code I use:

function mockedStore<TStoreDef extends () => unknown>(
  useStore: TStoreDef,
): TStoreDef extends StoreDefinition<infer Id, infer State, infer Getters, infer Actions>
  ? Store<
      Id,
      State,
      Record<string, never>,
      {
        [K in keyof Actions]: Actions[K] extends (...args: infer Args) => infer ReturnT
          ? Mock<Args, ReturnT>
          : Actions[K]
      }
    > & {
      [K in keyof Getters]: Getters[K] extends ComputedRef<infer T> ? T : never
    }
  : ReturnType<TStoreDef> {
  return useStore() as any
}

Usage:

const useStore = defineStore('...', () => {})
const store = mockedStore(useStore)

I need to add a page for this in the docs…

Replies: 1 comment 1 reply

Comment options

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

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