Skip to content

Commit

Permalink
fix(types): correctly type global extensions
Browse files Browse the repository at this point in the history
Fix #630
  • Loading branch information
posva committed Sep 30, 2021
1 parent bd4b38d commit cdbdba5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/pinia/package.json
Expand Up @@ -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",
Expand Down
53 changes: 53 additions & 0 deletions 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<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>
}
}
2 changes: 2 additions & 0 deletions packages/pinia/src/index.ts
Expand Up @@ -69,3 +69,5 @@ export {
PiniaPlugin,
PiniaVuePlugin,
} from './vue2-plugin'

export * from './globalExtensions'
17 changes: 0 additions & 17 deletions packages/pinia/src/rootStore.ts
Expand Up @@ -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>
Expand Down

0 comments on commit cdbdba5

Please sign in to comment.