Skip to content

Plugins: Expose DefineStoreOptionsBase in PiniaCustomProperties #1247

Open
@b-strauss

Description

What problem is this solving

I wrote a plugin to solve #625. This is basically working, but the types of the stores defined in the new stores option in DefineStoreOptionsBase are not accessible inside PiniaCustomProperties.

import type {PiniaPluginContext, Store, StoreDefinition} from 'pinia';

declare module 'pinia' {
  export interface DefineStoreOptionsBase<S, Store> {
    stores?: Record<string, StoreDefinition>;
  }

  export interface PiniaCustomProperties<Id, S, G, A> {
    readonly stores: Readonly<Record<string, Store>>;
  }
}

export function piniaPluginStores (context: PiniaPluginContext) {
  return {
    get stores () {
      return Object.freeze(
        Object.entries(context.options.stores ?? {})
          .reduce<Record<string, Store>>((accumulator, [name, definition]) => {
            accumulator[name] = definition();
            return accumulator;
          }, {}),
      );
    },
  };
}

Proposed solution

If the store options would be accessible inside PiniaCustomProperties, the actual types of the concrete stores could be accessed with this.stores.

Describe alternatives you've considered

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions