@@ -4,7 +4,7 @@ const managedKeys = new Set();
4
4
const localStorageCache = new TrackedMap ( ) ;
5
5
6
6
// like JSON.parse() but all returned objects are frozen
7
- function jsonParseAndFreeze ( json ) {
7
+ function jsonParseAndFreeze ( json : string ) {
8
8
return JSON . parse ( json , ( key , value ) =>
9
9
typeof value === 'object' ? Object . freeze ( value ) : value
10
10
) ;
@@ -22,19 +22,23 @@ window.addEventListener('storage', function ({ key, newValue }) {
22
22
return ;
23
23
}
24
24
25
- localStorageCache . set ( key , jsonParseAndFreeze ( newValue ) ) ;
25
+ localStorageCache . set ( key , jsonParseAndFreeze ( newValue as string ) ) ;
26
26
} ) ;
27
27
28
- export default function localStorageDecoratorFactory ( ...args ) {
28
+ export default function localStorageDecoratorFactory ( ...args : unknown [ ] ) {
29
29
const isDirectDecoratorInvocation = isElementDescriptor ( ...args ) ;
30
30
const customLocalStorageKey = isDirectDecoratorInvocation
31
31
? undefined
32
32
: args [ 0 ] ;
33
33
34
- function localStorageDecorator ( target , key , descriptor ) {
34
+ function localStorageDecorator (
35
+ target : unknown ,
36
+ key : PropertyKey ,
37
+ descriptor : PropertyDecorator & { initializer : ( ) => void }
38
+ ) {
35
39
const localStorageKey = customLocalStorageKey ?? key ;
36
40
37
- initalizeLocalStorageKey ( localStorageKey ) ;
41
+ initalizeLocalStorageKey ( localStorageKey as string ) ;
38
42
39
43
// register getter and setter
40
44
return {
@@ -46,21 +50,22 @@ export default function localStorageDecoratorFactory(...args) {
46
50
: undefined )
47
51
) ;
48
52
} ,
49
- set ( value ) {
53
+ set ( value : unknown ) {
50
54
const json = JSON . stringify ( value ) ;
51
55
52
56
// Update local storage cache. It must include a froozen copy the
53
57
// the value to prevent leaking state between different consumers.
54
58
localStorageCache . set ( localStorageKey , jsonParseAndFreeze ( json ) ) ;
55
59
56
60
// Update local storage.
57
- window . localStorage . setItem ( localStorageKey , json ) ;
61
+ window . localStorage . setItem ( localStorageKey as string , json ) ;
58
62
} ,
59
63
} ;
60
64
}
61
65
62
66
return isDirectDecoratorInvocation
63
- ? localStorageDecorator ( ...args )
67
+ ? // @ts -expect-error A spread argument must either have a tuple type or be passed to a rest parameter.
68
+ localStorageDecorator ( ...args )
64
69
: localStorageDecorator ;
65
70
}
66
71
@@ -69,7 +74,7 @@ export function clearLocalStorageCache() {
69
74
localStorageCache . clear ( ) ;
70
75
}
71
76
72
- export function initalizeLocalStorageKey ( key ) {
77
+ export function initalizeLocalStorageKey ( key : string ) {
73
78
// Check if key is already managed. If it is not managed yet, initialize it
74
79
// in localStorageCache with the current value in local storage.
75
80
// Need to use a separate, not tracked data store to do this check
@@ -79,7 +84,7 @@ export function initalizeLocalStorageKey(key) {
79
84
managedKeys . add ( key ) ;
80
85
localStorageCache . set (
81
86
key ,
82
- jsonParseAndFreeze ( window . localStorage . getItem ( key ) )
87
+ jsonParseAndFreeze ( window . localStorage . getItem ( key ) as string )
83
88
) ;
84
89
}
85
90
}
@@ -88,8 +93,8 @@ export function initalizeLocalStorageKey(key) {
88
93
//
89
94
// Borrowed from the Ember Data source code:
90
95
// https://github.com/emberjs/data/blob/22a8f20e2f11ed82c85160944e976073dc530d8b/packages/model/addon/-private/util.ts#L5
91
- function isElementDescriptor ( ...args ) {
92
- let [ maybeTarget , maybeKey , maybeDescriptor ] = args ;
96
+ function isElementDescriptor ( ...args : unknown [ ] ) {
97
+ const [ maybeTarget , maybeKey , maybeDescriptor ] = args ;
93
98
94
99
return (
95
100
// Ensure we have the right number of args
0 commit comments