Skip to content

Commit

Permalink
feat(types): allow extending mapStores suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Apr 9, 2021
1 parent 84f4c0a commit f14c7b9
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 2,800 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ dist
package-lock.json
.DS_Store
temp
test-dts/tsconfig.tsbuildinfo
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ export {
DefineStoreOptions,
} from './types'

export { mapActions, mapStores, mapState, mapGetters } from './mapHelpers'
export {
mapActions,
mapStores,
mapState,
mapGetters,
MapStoresCustomization,
} from './mapHelpers'

// TODO: remove in beta
export { createStore } from './deprecated'
16 changes: 13 additions & 3 deletions src/mapHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import type Vue from 'vue'
import {
GenericStore,
GenericStoreDefinition,
Method,
StateTree,
Store,
StoreDefinition,
} from './types'

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface MapStoresCustomization {
// suffix?: string
}

type StoreObject<S> = S extends StoreDefinition<
infer Ids,
infer State,
infer Getters,
infer Actions
>
? {
[Id in `${Ids}Store`]: () => Store<
Id extends `${infer RealId}Store` ? RealId : string,
[Id in `${Ids}${'suffix' extends keyof MapStoresCustomization
? MapStoresCustomization['suffix']
: 'Store'}`]: () => Store<
Id extends `${infer RealId}${'suffix' extends keyof MapStoresCustomization
? MapStoresCustomization['suffix']
: 'Store'}`
? RealId
: string,
State,
Getters,
Actions
Expand Down
21 changes: 21 additions & 0 deletions test-dts/customizations.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { defineStore, expectType, mapStores } from '.'

declare module '../dist/src/index' {
export interface MapStoresCustomization {
// this is the only one that can be applied to work with other tests
suffix: 'Store'
}
}

const useCounter = defineStore({
id: 'counter',
state: () => ({ n: 0 }),
})

type CounterStore = ReturnType<typeof useCounter>

const computedStores = mapStores(useCounter)

expectType<{
counterStore: () => CounterStore
}>(computedStores)
3 changes: 1 addition & 2 deletions test-dts/mapHelpers.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { mapActions, mapState } from 'dist/src'
import { defineStore, expectType, mapStores } from '.'
import { defineStore, expectType, mapStores, mapActions, mapState } from '.'

const useStore = defineStore({
id: 'name',
Expand Down
Loading

0 comments on commit f14c7b9

Please sign in to comment.