@@ -35,7 +35,7 @@ export class Collection<T extends object> {
35
35
}
36
36
37
37
return (
38
- new ( this . constructor as new ( items : typeof this . items ) => typeof this ) ( this . items ) . pluck ( key ) . sum ( ) /
38
+ new ( this . constructor as new ( items : typeof this . items ) => typeof this ) ( this . items ) . pluck ( key ) . sum ( ... [ ] ) /
39
39
this . items . length
40
40
)
41
41
}
@@ -49,7 +49,7 @@ export class Collection<T extends object> {
49
49
if ( Array . isArray ( this . items ) ) {
50
50
do {
51
51
const items = this . items . slice ( index , index + size )
52
- const collection = new this . constructor ( items )
52
+ const collection = new ( this . constructor as new ( items : typeof this . items ) => typeof this ) ( items )
53
53
54
54
chunks . push ( collection )
55
55
index += size
@@ -59,36 +59,36 @@ export class Collection<T extends object> {
59
59
60
60
do {
61
61
const keysOfChunk = keys . slice ( index , index + size )
62
- const collection = new this . constructor ( { } )
62
+ const collection = new ( this . constructor as new ( items : typeof this . items ) => typeof this ) ( { } as T [ ] )
63
63
64
- keysOfChunk . forEach ( ( key ) => collection . put ( key , this . items [ key ] ) )
64
+ keysOfChunk . forEach ( ( key : string ) => collection . put ( key , ( this . items as Record < string , any > ) [ key ] ) )
65
65
66
66
chunks . push ( collection )
67
67
index += size
68
68
} while ( index < keys . length )
69
69
} else {
70
- chunks . push ( new this . constructor ( [ this . items ] ) )
70
+ chunks . push ( new ( this . constructor as new ( items : typeof this . items ) => typeof this ) ( [ this . items ] ) )
71
71
}
72
72
73
- return new this . constructor ( chunks )
73
+ return new ( this . constructor as new ( items : T [ ] [ ] ) => Collection < T [ ] > ) ( chunks as unknown as T [ ] [ ] )
74
74
}
75
75
76
- collapse ( ) : Collection < T > {
77
- return new this . constructor ( [ ] . concat ( ...this . items ) )
76
+ collapse ( ) : Collection < T extends any [ ] ? T [ number ] : T > {
77
+ return new ( this . constructor as any ) ( ( [ ] as T [ ] ) . concat ( ...( this . items as any [ ] ) ) )
78
78
}
79
79
80
80
combine ( array : Collection < T > | T [ ] ) : Collection < T > {
81
81
let values = array
82
82
83
83
if ( values instanceof this . constructor ) {
84
- values = array . all ( )
84
+ values = ( values as Collection < T > ) . all ( )
85
85
}
86
86
87
87
const collection = { }
88
88
89
89
if ( Array . isArray ( this . items ) && Array . isArray ( values ) ) {
90
90
this . items . forEach ( ( key , iterator ) => {
91
- collection [ key ] = values [ iterator ]
91
+ ; ( collection as Record < string , unknown > ) [ String ( key ) ] = values [ iterator ]
92
92
} )
93
93
} else if ( typeof this . items === 'object' && typeof values === 'object' ) {
94
94
Object . keys ( this . items ) . forEach ( ( key , index ) => {
@@ -334,21 +334,21 @@ export class Collection<T extends object> {
334
334
return this
335
335
}
336
336
337
- eachSpread ( fn ) {
338
- this . each ( ( values , key ) => {
337
+ eachSpread ( fn : ( ... args : any [ ] ) => void ) : Collection < T > {
338
+ this . each ( ( values : any , key : any ) => {
339
339
fn ( ...values , key )
340
340
} )
341
341
342
342
return this
343
343
}
344
344
345
- every ( fn ) {
345
+ every ( fn : ( value : T , key : string | number , collection : T [ ] ) => boolean ) : boolean {
346
346
const items = values ( this . items )
347
347
348
348
return items . every ( fn )
349
349
}
350
350
351
- except ( ...args ) {
351
+ except ( ...args : any [ ] ) : Collection < T > {
352
352
const properties = variadic ( args )
353
353
354
354
if ( Array . isArray ( this . items ) ) {
@@ -368,7 +368,7 @@ export class Collection<T extends object> {
368
368
return new this . constructor ( collection )
369
369
}
370
370
371
- filter ( fn ) {
371
+ filter ( fn : ( value : T , key : string | number , collection : T [ ] ) => boolean ) : Collection < T > {
372
372
const func = fn || false
373
373
let filteredItems = null
374
374
if ( Array . isArray ( this . items ) ) {
@@ -380,7 +380,7 @@ export class Collection<T extends object> {
380
380
return new this . constructor ( filteredItems )
381
381
}
382
382
383
- first ( fn , defaultValue ) {
383
+ first ( fn : ( value : T , key : string | number , collection : T [ ] ) => boolean , defaultValue : T ) : T {
384
384
if ( isFunction ( fn ) ) {
385
385
const keys = Object . keys ( this . items )
386
386
@@ -417,7 +417,7 @@ export class Collection<T extends object> {
417
417
return defaultValue
418
418
}
419
419
420
- firstOrFail ( key , operator , value ) {
420
+ firstOrFail ( key : string , operator : string , value : any ) : T {
421
421
if ( isFunction ( key ) ) {
422
422
return this . first ( key , ( ) => {
423
423
throw new Error ( 'Item not found.' )
@@ -433,7 +433,7 @@ export class Collection<T extends object> {
433
433
return collection . first ( )
434
434
}
435
435
436
- firstWhere ( key , operator , value ) {
436
+ firstWhere ( key : string , operator : string , value : any ) : T {
437
437
return this . where ( key , operator , value ) . first ( ) || null
438
438
}
439
439
@@ -1011,10 +1011,9 @@ export class Collection<T extends object> {
1011
1011
return fn ( this )
1012
1012
}
1013
1013
1014
- pluck ( value , key ) {
1014
+ pluck ( value : string , key ?: string ) {
1015
1015
if ( value . indexOf ( '*' ) !== - 1 ) {
1016
1016
const keyPathMap = buildKeyPathMap ( this . items )
1017
-
1018
1017
const keyMatches = [ ]
1019
1018
1020
1019
if ( key !== undefined ) {
@@ -1594,7 +1593,7 @@ export class Collection<T extends object> {
1594
1593
return new this . constructor ( collection )
1595
1594
}
1596
1595
1597
- sum ( key ) {
1596
+ sum ( key ?: string | ( ( item : T ) => number ) ) : number {
1598
1597
const items = values ( this . items )
1599
1598
1600
1599
let total = 0
0 commit comments