From cdbdba5153198cde6b678cb96ab7948b351fd3cc Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 30 Sep 2021 16:08:15 +0200 Subject: [PATCH] fix(types): correctly type global extensions Fix #630 --- packages/pinia/package.json | 2 +- packages/pinia/src/globalExtensions.ts | 53 ++++++++++++++++++++++++++ packages/pinia/src/index.ts | 2 + packages/pinia/src/rootStore.ts | 17 --------- 4 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 packages/pinia/src/globalExtensions.ts diff --git a/packages/pinia/package.json b/packages/pinia/package.json index 4a039da6..95e01b0c 100644 --- a/packages/pinia/package.json +++ b/packages/pinia/package.json @@ -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 index 00000000..432ff15d --- /dev/null +++ b/packages/pinia/src/globalExtensions.ts @@ -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 + } +} + +// @ts-ignore: works on Vue 2, fails in Vue 3 +declare module 'vue/types/options' { + interface ComponentOptions { + /** + * 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 + } +} diff --git a/packages/pinia/src/index.ts b/packages/pinia/src/index.ts index 4058d684..93872952 100644 --- a/packages/pinia/src/index.ts +++ b/packages/pinia/src/index.ts @@ -69,3 +69,5 @@ export { PiniaPlugin, PiniaVuePlugin, } from './vue2-plugin' + +export * from './globalExtensions' diff --git a/packages/pinia/src/rootStore.ts b/packages/pinia/src/rootStore.ts index 15ff28e1..8181e234 100644 --- a/packages/pinia/src/rootStore.ts +++ b/packages/pinia/src/rootStore.ts @@ -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 - } -} - export const piniaSymbol = ( __DEV__ ? Symbol('pinia') : /* istanbul ignore next */ Symbol() ) as InjectionKey -- 2.47.3