1
1
import type { DocumentData , DocumentReference } from 'firebase/firestore'
2
2
3
+ // FIXME: replace any with unknown or T generics
4
+
3
5
export interface OperationsType {
4
- set : ( target : Record < string , any > , key : string | number , value : any ) => any
5
- add : ( array : any [ ] , index : number , data : DocumentData ) => any
6
- remove : ( array : any [ ] , index : number ) => any
6
+ set < T extends object = Record < any , unknown > > (
7
+ target : T ,
8
+ // accepts a dot delimited path
9
+ path : string | number ,
10
+ value : T [ any ]
11
+ ) : T [ any ] | T [ any ] [ ]
12
+ add < T extends unknown = unknown > ( array : T [ ] , index : number , data : T ) : T [ ]
13
+ remove < T extends unknown = unknown > ( array : T [ ] , index : number ) : T [ ]
7
14
}
8
15
9
16
export type ResetOption = boolean | ( ( ) => any )
@@ -28,13 +35,13 @@ export function walkGet(obj: Record<string, any>, path: string): any {
28
35
* @param value
29
36
* @returns an array with the element that was replaced or the value that was set
30
37
*/
31
- export function walkSet < T > (
32
- obj : Record < string , any > ,
38
+ export function walkSet < T extends object = Record < any , unknown > > (
39
+ obj : T ,
33
40
path : string | number ,
34
- value : T
35
- ) : T | T [ ] {
41
+ value : T [ any ]
42
+ ) : T [ any ] | T [ any ] [ ] {
36
43
// path can be a number
37
- const keys = ( '' + path ) . split ( '.' )
44
+ const keys = ( '' + path ) . split ( '.' ) as Array < keyof T >
38
45
const key = keys . pop ( ) as string // split will produce at least one element array
39
46
const target = keys . reduce (
40
47
( target , key ) : any =>
0 commit comments