Vuex: useStore is missing a type declaration

Created on 29 Apr 2020  路  4Comments  路  Source: vuejs/vuex

Version

4.0.0-beta.1

Reproduction link

https://github.com/ChrisShank/vue-next-webpack-preview

Steps to reproduce

Remove the type declaration of 'useStore' I added in 'vuex-shim.d.ts', and there will be a compile-time TS error.

What is expected?

The type for useStore to exist.

What is actually happening?

On the line:

import { useStore } from 'vuex'

There is an error:

TS2614: Module 'vue-next-webpack-preview/node_modules/vuex/types"' has no exported member 'useStore'. Did you mean to use 'import useStore from "vue-next-webpack-preview/node_modules/vuex/types"' instead?
4.0 bug types

Most helpful comment

It seems that the type is difficult to cover, you can do this:

// @ts-ignore
import { createStore, useStore as useVuexStore } from 'vuex'

const store =  createStore<StoreState>({})

export default store
export function useStore(): typeof store {
  return useVuexStore()
}

All 4 comments

@ChrisShank Override the vuex's type definition in TypeScript.

declare module "vuex" {
  function useStore<T = any>(key?: string): T
}

It seems that the type is difficult to cover, you can do this:

// @ts-ignore
import { createStore, useStore as useVuexStore } from 'vuex'

const store =  createStore<StoreState>({})

export default store
export function useStore(): typeof store {
  return useVuexStore()
}

@surmon-china I currently am here

I think that a type should either be included with the package (similar to the one you first mentioned) or documented that the user is expected to provide their own shim.

Thanks for the report! Yea we must add this one 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

matthewmorgan picture matthewmorgan  路  3Comments

haoxins picture haoxins  路  4Comments

jbruni picture jbruni  路  3Comments

fnlctrl picture fnlctrl  路  4Comments

niallobrien picture niallobrien  路  3Comments