项目作者: sascha245

项目描述 :
Write fully type inferred Vuex modules
高级语言: TypeScript
项目地址: git://github.com/sascha245/vuex-context.git
创建时间: 2019-03-16T09:16:54Z
项目社区:https://github.com/sascha245/vuex-context

开源协议:MIT License

下载


vuex-context

Write fully type inferred Vuex modules:

  • Extremely lightweight
  • No boilerplate
  • No class
  • Handles module refactoring

Install

  1. Install vuex
    npm install vuex --save

  2. Install module:
    npm install vuex-context --save

Usage

  1. Write your Vuex modules in the standard Vuex way:
  1. export interface CounterState {
  2. count: number;
  3. }
  4. export const namespaced = true;
  5. export const state = (): CounterState => ({
  6. count: 0
  7. });
  8. export const mutations = {
  9. increment(state: CounterState) {
  10. state.count++;
  11. },
  12. incrementBy(state: CounterState, payload: number) {
  13. state.count += payload;
  14. }
  15. };
  16. export const actions = {
  17. async incrementAsync(context) {
  18. // ...
  19. }
  20. };
  21. export const getters = {
  22. doubleCount(state: CounterState): number {
  23. return state.count * 2;
  24. },
  25. quadrupleCount(state: CounterState, context): number {
  26. // ...
  27. }
  28. }
  1. Create the module’s context
  1. export const Counter = Context.fromModule({
  2. state,
  3. mutations,
  4. actions,
  5. getters
  6. });
  1. That’s it, now you have access to a completely typed version of your module
  1. // Use store and the namespace leading to the module to get a new context instance
  2. const counterModule = Counter.getInstance(store, 'counter');
  3. counterModule.dispatch.incrementAsync();
  4. // Or use the context to access the current scope
  5. export const actions = {
  6. async incrementAsync(context) {
  7. const counterModule = Counter.getInstance(context);
  8. counterModule.commit.increment();
  9. }
  10. };
  11. // You can also type your getters
  12. export const getters = {
  13. doubleCount(state: CounterState): number {
  14. return state.count * 2;
  15. },
  16. quadrupleCount(state: CounterState, context): number {
  17. const getters = Counter.getGetters(context);
  18. return getters.doubleCount * 2;
  19. }
  20. }

Circular references

Warning: Be careful when returning values from your actions and getters!

  1. export const actions = {
  2. async incrementAsync(context) {
  3. const counterModule = Counter.getInstance(context);
  4. counterModule.commit.increment();
  5. // Circular reference here, as incrementAsync needs the type from counterModule and counterModule needs the type from incrementAsync
  6. // Result: counterModule is cast to any
  7. return counterModule.state.count;
  8. }
  9. };

To avoid this, you should manually write the return types of your actions and getters:

  1. export const actions = {
  2. // Specify the return type here
  3. async incrementAsync(context): Promise<number> {
  4. const counterModule = Counter.getInstance(context);
  5. counterModule.commit.increment();
  6. // Everything works fine now
  7. return counterModule.state.count;
  8. }
  9. };

Contributors

If you are interested and want to help out, don’t hesitate to contact me or to create a pull request with your fixes / features.

The project now also contains samples that you can use to directly test out your features during development.

  1. Clone the repository

  2. Install dependencies
    npm install

  3. Launch samples
    npm run serve

  4. Launch unit tests situated in ./tests. The unit tests are written in Jest.
    npm run test:unit

License

This project is licensed under the MIT License - see the LICENSE.md file for details