Skip to content

Commit 08bd464

Browse files
committed
test: types
1 parent ca73db9 commit 08bd464

File tree

2 files changed

+151
-119
lines changed

2 files changed

+151
-119
lines changed

packages/pinia/test-dts/store.test-d.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { StoreGeneric, defineStore, expectType } from './'
2-
import { watch } from 'vue'
2+
import { UnwrapRef, watch } from 'vue'
33

44
const 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
84110
defineStore({
85111
id: '',

0 commit comments

Comments
 (0)