]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
fix(types): correctly type global extensions
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 30 Sep 2021 14:08:15 +0000 (16:08 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Thu, 30 Sep 2021 14:08:15 +0000 (16:08 +0200)
Fix #630

packages/pinia/package.json
packages/pinia/src/globalExtensions.ts [new file with mode: 0644]
packages/pinia/src/index.ts
packages/pinia/src/rootStore.ts

index 4a039da6e2adc3cc4da25949c0e975788978a891..95e01b0cebb7e98d75b01c918b7ed30dd25c2a34 100644 (file)
@@ -26,7 +26,7 @@
   "funding": "https://github.com/sponsors/posva",
   "scripts": {
     "build": "rimraf dist && rollup -c ../../rollup.config.js --environment TARGET:pinia",
-    "build:dts": "api-extractor run --local --verbose",
+    "build:dts": "api-extractor run --local --verbose && tail -n +3 ./src/globalExtensions.ts >> dist/pinia.d.ts",
     "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s --commit-path . -l pinia -r 1",
     "test:dts": "tsc -p ./test-dts/tsconfig.json",
     "dev": "yarn run test:unit --watchAll",
diff --git a/packages/pinia/src/globalExtensions.ts b/packages/pinia/src/globalExtensions.ts
new file mode 100644 (file)
index 0000000..432ff15
--- /dev/null
@@ -0,0 +1,53 @@
+import type { Pinia } from './rootStore'
+import type { Store, StoreGeneric } from './types'
+
+// Extensions of Vue types to be appended manually
+// https://github.com/microsoft/rushstack/issues/2090
+// https://github.com/microsoft/rushstack/issues/1709
+
+// @ts-ignore: works on Vue 2, fails in Vue 3
+declare module 'vue/types/vue' {
+  interface Vue {
+    /**
+     * Currently installed pinia instance.
+     */
+    $pinia: Pinia
+
+    /**
+     * Cache of stores instantiated by the current instance. Used by map
+     * helpers.
+     *
+     * @internal
+     */
+    _pStores?: Record<string, Store>
+  }
+}
+
+// @ts-ignore: works on Vue 2, fails in Vue 3
+declare module 'vue/types/options' {
+  interface ComponentOptions<V> {
+    /**
+     * Pinia instance to install in your application. Should be passed to the
+     * root Vue.
+     */
+    pinia?: Pinia
+  }
+}
+
+// @ts-ignore: works on Vue 3, fails in Vue 2
+declare module '@vue/runtime-core' {
+  export interface ComponentCustomProperties {
+    /**
+     * Access to the application's Pinia
+     */
+    $pinia: Pinia
+
+    /**
+     * Cache of stores instantiated by the current instance. Used by devtools to
+     * list currently used stores.
+     *
+     * @internal
+     */
+    _pStores?: Record<string, StoreGeneric>
+  }
+}
index 4058d684ee96cbffc4e82d6b2c5400487cd21a74..9387295212f9e9dfe90d81487539c19b2cf7daed 100644 (file)
@@ -69,3 +69,5 @@ export {
   PiniaPlugin,
   PiniaVuePlugin,
 } from './vue2-plugin'
+
+export * from './globalExtensions'
index 15ff28e10ce5c53361544da4e0206b286b1fe8da..8181e234cb9c3dfdde4e3d9534d19e3a4643f065 100644 (file)
@@ -94,23 +94,6 @@ export interface Pinia {
   _testing?: boolean
 }
 
-declare module '@vue/runtime-core' {
-  export interface ComponentCustomProperties {
-    /**
-     * Access to the application's Pinia
-     */
-    $pinia: Pinia
-
-    /**
-     * Cache of stores instantiated by the current instance. Used by devtools to
-     * list currently used stores.
-     *
-     * @internal
-     */
-    _pStores?: Record<string, StoreGeneric>
-  }
-}
-
 export const piniaSymbol = (
   __DEV__ ? Symbol('pinia') : /* istanbul ignore next */ Symbol()
 ) as InjectionKey<Pinia>