vuex icon indicating copy to clipboard operation
vuex copied to clipboard

feat(types): strictly typed

Open kotamat opened this issue 3 years ago • 2 comments

Using StoreObject instead of State allows for more type-safe calls to the Vuex API from client.

  • store.dispatch(), store.commit() has namespaced-typed params and their payloads
    • the params links to definition on VS Code
  • The inside of StoreObject-calls(ex, commit()) may have any type
  • mapXxx() are not changed.
  • Not supported root, namespaced flags

4f3d2b20c56f96ba1e23a6b9a77c60e7 34510fb278105f5d3e755a367be76eeb

and changed code formats.

Please see

  • the naming( because of poor English skill..)
  • formats ( default prettier format attached )
  • some omissions (don't know well whole Vuex codes)

kotamat avatar Mar 22 '21 13:03 kotamat

Drive-by comment: thanks for working on this! I don't understand the generics involved in ExtractObjects, but I'm wondering if it would be possible to skip the separate S type param to useStore and also extract that from SO?

You retracted pull/1943, which seems to be the same thing, but doing the above S extraction (and splitting off the reformatting from the individual changes). I'm just curious why you replaced the PR.

tommie avatar Apr 06 '21 14:04 tommie

@tommie ExtractObjects transforms StoreObject to useful type for args/return-value of dispatch() and commit(). like this

const s = {
  modules: {
    foo: {
      modules: {
        hoge: {
          actions: {
            bar: (n: number) => "aaa",
          },
        },
      },
    },
    bar: {
      actions: {
        bal: (x: string) => "aaa",
      },
    },
  },
};

to

{
  "bar/bal": (x: string) => string,
  "foo/hoge/bar": (n: number) => string
}

I'm wondering if it would be possible to skip the separate S type param to useStore and also extract that from SO?

Yes.

You can skip like this 22e706a7818996ead6cda631dc8788e3

// store is instance of vuex
  const key: InjectionKey<typeof store> = Symbol('key')
  const usedStore = Vuex.useStore(key)

You retracted pull/1943, which seems to be the same thing, but doing the above S extraction (and splitting off the reformatting from the individual changes). I'm just curious why you replaced the PR.

Initially, I have intended to send out the PR to my repo, but I made a mistake and created 1943. So, I reflexively closed it. Then, I have finished the changes, cleaned-up the commit messages and made this PR.

kotamat avatar Apr 06 '21 14:04 kotamat