1
- import { clone , deleteKeys , isArray , isFunction , isObject , nestedValue , values , variadic } from './helpers/'
1
+ import { clone , deleteKeys , isArray , isFunction , isObject , nestedValue , values } from './helpers/'
2
2
3
3
export class Collection < T > {
4
4
protected items : T [ ] | Record < string , T >
@@ -13,8 +13,8 @@ export class Collection<T> {
13
13
return this . items
14
14
}
15
15
16
- all ( ) : T [ ] | Record < string , T > {
17
- return this . items
16
+ all ( ) : CollectionAll < T > {
17
+ return this . items as CollectionAll < T >
18
18
}
19
19
20
20
average ( key ?: string | ( ( item : T ) => number ) ) : number {
@@ -48,9 +48,11 @@ export class Collection<T> {
48
48
return new ( this . constructor as any ) ( ( [ ] as T [ ] ) . concat ( ...( this . items as any [ ] ) ) )
49
49
}
50
50
51
- combine ( values : T [ ] ) : Collection < T > {
51
+ combine < U extends unknown [ ] > ( values : U ) : Collection < { [ K in T [ number ] ] : U [ number ] } > {
52
52
if ( Array . isArray ( this . items ) && Array . isArray ( values ) ) {
53
- return new Collection ( Object . fromEntries ( this . items . map ( ( key , index ) => [ key , values [ index ] ] ) ) )
53
+ return new Collection ( Object . fromEntries ( this . items . map ( ( key , index ) => [ key , values [ index ] ] ) ) ) as Collection < {
54
+ [ K in T [ number ] ] : U [ number ]
55
+ } >
54
56
}
55
57
56
58
throw new Error ( 'Cannot combine non-array collections' )
@@ -1949,7 +1951,11 @@ function SymbolIterator() {
1949
1951
}
1950
1952
}
1951
1953
1952
- export const collect = < T extends object | number > ( collection ?: T | T [ ] | Collection < T > ) : Collection < T > =>
1953
- new Collection < T > ( collection )
1954
+ export function collect < T extends object | any [ ] > ( items : T ) : Collection < T > {
1955
+ return new Collection ( items )
1956
+ }
1957
+
1958
+ type IsArray < T > = T extends any [ ] ? true : false
1959
+ type CollectionAll < T > = IsArray < T > extends true ? T : { [ K in keyof T ] : T [ K ] }
1954
1960
1955
1961
export default collect
0 commit comments