File tree Expand file tree Collapse file tree 2 files changed +151
-119
lines changed
Expand file tree Collapse file tree 2 files changed +151
-119
lines changed Original file line number Diff line number Diff line change 11import { StoreGeneric , defineStore , expectType } from './'
2- import { watch } from 'vue'
2+ import { UnwrapRef , watch } from 'vue'
33
44const useStore = defineStore ( {
55 id : 'name' ,
@@ -80,6 +80,32 @@ defineStore({
8080 } ,
8181} )
8282
83+ interface Model {
84+ id : number
85+ }
86+
87+ // Define generic factory function
88+ export function init < User extends Model > ( name = 'settings' ) {
89+ return defineStore ( name , {
90+ state : ( ) => {
91+ return {
92+ // Set one of the properties to the generic type
93+ user : { } as User ,
94+ }
95+ } ,
96+ actions : {
97+ // Add action which accepts argument with our generic type
98+ set ( u : UnwrapRef < User > ) {
99+ // See linter error when trying to assign arg value to the state
100+ this . user = u
101+ } ,
102+ } ,
103+ } )
104+ }
105+
106+ const s = init ( ) ( )
107+ s . set ( { id : 1 } )
108+
83109// getters on not existing properties
84110defineStore ( {
85111 id : '' ,
You can’t perform that action at this time.
0 commit comments