32
32
* @returns An Object with the new values returned by `fn`.
33
33
*/
34
34
export function treeMap ( func : ( ...args : any [ ] ) => unknown ,
35
- tree : unknown ,
36
- rest ?: object [ ] ,
35
+ tree : Record < string , any > ,
36
+ rest ?: Record < string , any > [ ] ,
37
37
isLeaf ?: ( node : unknown ) => boolean ) : unknown {
38
38
if ( isLeaf && isLeaf ( tree ) ) {
39
39
if ( rest )
@@ -43,7 +43,7 @@ export function treeMap(func: (...args: any[]) => unknown,
43
43
} else if ( Array . isArray ( tree ) ) {
44
44
return tree . map ( ( child , i ) => treeMap ( func , child , rest ?. map ( r => r [ i ] ) , isLeaf ) ) ;
45
45
} else if ( typeof tree === 'object' && isDict ( tree ) ) {
46
- const newTree = { } ;
46
+ const newTree : Record < string , any > = { } ;
47
47
for ( const k in tree ) {
48
48
newTree [ k ] = treeMap ( func , tree [ k ] , rest ?. map ( r => r [ k ] ) , isLeaf ) ;
49
49
}
@@ -84,8 +84,8 @@ export function treeMap(func: (...args: any[]) => unknown,
84
84
* ```
85
85
*/
86
86
export function treeMapWithPath ( fn : ( path ?: string , ...args : unknown [ ] ) => unknown ,
87
- tree : unknown ,
88
- rest ?: object [ ] ,
87
+ tree : Record < string , any > ,
88
+ rest ?: Record < string , any > [ ] ,
89
89
isLeaf ?: ( node : unknown ) => boolean ,
90
90
path ?: string ) : unknown {
91
91
if ( isLeaf && isLeaf ( tree ) ) {
@@ -96,7 +96,7 @@ export function treeMapWithPath(fn: (path?: string, ...args: unknown[]) => unkno
96
96
} else if ( Array . isArray ( tree ) ) {
97
97
return tree . map ( ( child , i ) => treeMapWithPath ( fn , child , rest ?. map ( r => r [ i ] ) , isLeaf , path ? `${ path } .${ i } ` : `${ i } ` ) ) ;
98
98
} else if ( typeof tree === 'object' && isDict ( tree ) ) {
99
- const newTree = { } ;
99
+ const newTree : Record < string , any > = { } ;
100
100
for ( const k in tree ) {
101
101
newTree [ k ] = treeMapWithPath ( fn , tree [ k ] , rest ?. map ( r => r [ k ] ) , isLeaf , path ? `${ path } .${ k } ` : `${ k } ` ) ;
102
102
}
@@ -138,7 +138,7 @@ export function treeMapWithPath(fn: (path?: string, ...args: unknown[]) => unkno
138
138
* // Outputs: [['hello.0.0.0', 0]]
139
139
* ```
140
140
*/
141
- export function treeFlatten ( tree : unknown ,
141
+ export function treeFlatten ( tree : Record < string , any > ,
142
142
prefix : string = '' ,
143
143
isLeaf ?: ( node : unknown ) => boolean ,
144
144
convertKey ?: ( key : string ) => string ) : [ string , unknown ] [ ] {
@@ -199,12 +199,12 @@ export function treeUnflatten(tree: [string, unknown][],
199
199
// Recursively map them to the original container.
200
200
if ( isList ) {
201
201
const keys = Object . keys ( children ) . sort ( ) . map ( ( idx ) => [ parseInt ( idx ) , idx ] ) ;
202
- const newList = [ ] ;
202
+ const newList : any [ ] = [ ] ;
203
203
for ( const [ i , k ] of keys )
204
- newList [ i ] = treeUnflatten ( children [ k ] , convertKey ) ;
204
+ newList [ i as number ] = treeUnflatten ( children [ k ] , convertKey ) ;
205
205
return newList ;
206
206
} else {
207
- const newTree = { } ;
207
+ const newTree : Record < string , any > = { } ;
208
208
for ( let k in children ) {
209
209
const key = convertKey ? convertKey ( k ) : k ;
210
210
newTree [ key ] = treeUnflatten ( children [ k ] , convertKey ) ;
0 commit comments