FreeTube icon indicating copy to clipboard operation
FreeTube copied to clipboard

Add typings for the Vuex store

Open absidue opened this issue 1 year ago • 4 comments

Add typings for the Vuex store

Pull Request Type

  • [x] Feature Implementation
  • [x] Developer Improvement

Description

I know we plan to eventually switch to Pinia, which has first-class TypeScript support out-of-the-box but until then I thought I would help us out a bit. I've been working on this on and off for a few weeks now and am now finally happy enough with it to create this pull request.

VueX like Vue 2 was designed before everyone realised that auto-complete and type safety are unmissable development aides, so neither was designed with TypeScript in mind. For Vue 2 they've done a very good job of creating mostly usable types, despite the Options API being extremely difficult to type (some of the types they have built are really complex), for Vuex however there is very limited typing out of the box.

Most of the guides online recommended writing all the types for your Vuex store by hand and maintaining them by hand, which I thought was a bit ridiculous to do for FreeTube as we don't do any type checking at the moment and are probably going to switch to Pinia at some point in the near future anyway. So I built this "magical" thing that infers most of the stuff automatically for you.

Caveats:

  • Due to bugs in Vue 2's types, it doesn't add anything in methods to this, unfortunately that means that despite mapActions and mapMutations returning the correct types, the mapped actions and mutations won't show up on this, like all other methods.
  • Even though this mostly works okay, there are some issues that can crop up due to everything being inferred and typescript getting confused, one such example is that we can't add typings to the side-effect handlers in the settings store module, otherwise TypeScript complains that the state and getters reference themselves.

Screenshots

map-actions-auto-complete map-mutations-auto-complete this-store-comit-parameter-types this-store-commit-auto-complete this-store-commit-parameterless this-store-dispatch-parameters this-store-dispatch-settings this-store-getters-auto-complete this-store-typed-getters

Testing

Try it out in your editor! (Please read the caveats section above if you haven't already)

Desktop

  • OS: Windows
  • OS Version: 10
  • FreeTube version: b2fe59d331289e0365f67e04e0186e17374b8d1c

absidue avatar Jul 10 '24 17:07 absidue

I use RubyMine & see auto complete but not the return types of getters Could be RubyMine issue though image

PikachuEXE avatar Jul 10 '24 22:07 PikachuEXE

This pull request has conflicts, please resolve those before we can evaluate the pull request.

github-actions[bot] avatar Aug 04 '24 13:08 github-actions[bot]

Conflicts have been resolved. A maintainer will review the pull request shortly.

github-actions[bot] avatar Aug 07 '24 21:08 github-actions[bot]

This pull request has conflicts, please resolve those before we can evaluate the pull request.

github-actions[bot] avatar Aug 23 '24 15:08 github-actions[bot]

Closing as it seems unlikely that this will get merged and hopefully we'll switch to Pinia soon which has much better typing support out of the box.

absidue avatar Aug 31 '24 14:08 absidue