Skip to content

Commit ccbd562

Browse files
committed
types: improved watchSharedData typing
1 parent d61d472 commit ccbd562

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

packages/shared-utils/src/shared-data.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const internalSharedData = {
3131
debugInfo: false,
3232
}
3333

34+
type TSharedData = typeof internalSharedData
35+
3436
const persisted = [
3537
'componentNameStyle',
3638
'theme',
@@ -182,7 +184,7 @@ export function destroySharedData () {
182184
watchers = {}
183185
}
184186

185-
let watchers = {}
187+
let watchers: Partial<Record<keyof TSharedData, ((value: any, oldValue: any) => unknown)[]>> = {}
186188

187189
function setValue (key: string, value: any) {
188190
// Storage
@@ -206,7 +208,9 @@ function sendValue (key: string, value: any) {
206208
})
207209
}
208210

209-
export function watchSharedData (prop, handler) {
211+
export function watchSharedData <
212+
TKey extends keyof TSharedData,
213+
> (prop: TKey, handler: (value: TSharedData[TKey], oldValue: TSharedData[TKey]) => unknown) {
210214
const list = watchers[prop] || (watchers[prop] = [])
211215
list.push(handler)
212216
return () => {

0 commit comments

Comments
 (0)