ClickerMonkey / vuex-typescript-interface

Adding magical type safety to Vuex without additional code
MIT License
44 stars 4 forks source link

Action payload optional prevents object destructuring #7

Closed mrkswrnr closed 5 years ago

mrkswrnr commented 5 years ago

The definition of ActionHandler marks payload as optional. This makes payload destructuring harder.

A mutation/action could require a payload, but at the moment it will appear optional.

Why did you choose this approach?

Example:

interface IStore
{
  fetch (payload: {id: number}): Promise<void>
}

const store = new Store<IStore>({
  actions: {
    // Property 'id' does not exist on type '{ id: number; } | undefined'. # ts(2339)
    async fetch ({ commit }, {id}) {
      // action
    }
  }
});
ClickerMonkey commented 5 years ago

Thanks for bringing this to my attention, working on it.

ClickerMonkey commented 5 years ago

Also, I chose this approach because when you're invoking an action/commit it might not have a payload, so to require one would be silly. I'll see if I can get proper payload existence detection working, and if possible I can get this working properly.

mrkswrnr commented 5 years ago

Thank you for working on it!

Also, I chose this approach because when you're invoking an action/commit it might not have a payload, so to require one would be silly.

I understand. I tried to come up with a conditional type that casts the payload parameter to void/undefined but while Typescript accepts omitting void/undefined parameters when declaring them directly, this does not seems to be working when using conditional types.

If there is no way to omit the parameter I would prefer to be forced to use an empty object as parameter instead of not being able to use object destructuring. But of course this is your decision as a module author.

ClickerMonkey commented 5 years ago

Resolved with v1.0.2