Skip to content

Commit fed591f

Browse files
Improved types
1 parent ac1a5d2 commit fed591f

38 files changed

Lines changed: 82 additions & 77 deletions

src/attributes/attr.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ interface Cash {
88
attr (): undefined;
99
attr ( attrs: string ): string | null;
1010
attr ( attrs: string, value: string ): this;
11-
attr ( attrs: plainObject ): this;
11+
attr ( attrs: Record<string, string> ): this;
1212
}
1313

1414
function attr ( this: Cash ): undefined;
1515
function attr ( this: Cash, attr: string ): string | null;
1616
function attr ( this: Cash, attr: string, value: string ): Cash;
17-
function attr ( this: Cash, attr: plainObject ): Cash;
18-
function attr ( this: Cash, attr?: string | plainObject, value?: string ) {
17+
function attr ( this: Cash, attr: Record<string, string> ): Cash;
18+
function attr ( this: Cash, attr?: string | Record<string, string>, value?: string ) {
1919

2020
if ( !attr ) return;
2121

src/attributes/has_class.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ interface Cash {
99

1010
fn.hasClass = function ( this: Cash, cls: string ) {
1111

12-
return !!cls && some.call ( this, ( ele: Ele ) => ele.classList.contains ( cls ) );
12+
return !!cls && some.call ( this, ( ele: EleLoose ) => ele.classList.contains ( cls ) );
1313

1414
};

src/attributes/prop.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
interface Cash {
77
prop ( prop: string ): any;
88
prop ( prop: string, value: any ): this;
9-
prop ( props: plainObject ): this;
9+
prop ( props: Record<string, any> ): this;
1010
}
1111

12-
fn.prop = function ( this: Cash, prop: string | plainObject, value?: any ) {
12+
fn.prop = function ( this: Cash, prop: string | Record<string, any>, value?: any ) {
1313

1414
if ( !prop ) return;
1515

src/collection/filter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ fn.filter = function ( this: Cash, comparator?: Comparator ) {
1313

1414
const compare = getCompareFunction ( comparator );
1515

16-
return cash ( filter.call ( this, ( ele: Ele, i: number ) => compare.call ( ele, i, ele ) ) );
16+
return cash ( filter.call ( this, ( ele: EleLoose, i: number ) => compare.call ( ele, i, ele ) ) );
1717

1818
};

src/collection/map.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ interface Cash {
1010

1111
fn.map = function ( this: Cash, callback: MapCallback<EleLoose> ) {
1212

13-
return cash ( map.call ( this, ( ele: Ele, i: number ) => callback.call ( ele, i, ele ) ) );
13+
return cash ( map.call ( this, ( ele: EleLoose, i: number ) => callback.call ( ele, i, ele ) ) );
1414

1515
};

src/core/each.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface CashStatic {
77
each<T> ( arr: ArrayLike<T>, callback: EachCallback<T> ): void;
88
}
99

10-
function each<T> ( arr: ArrayLike<T>, callback: EachCallback<T>, reverse?: boolean ): ArrayLike<T> { //TODO: Maybe add a flag for looping in reverse
10+
function each<T, U extends ArrayLike<T> = ArrayLike<T>> ( arr: U, callback: EachCallback<U[0]>, reverse?: boolean ): U {
1111

1212
if ( reverse ) {
1313

src/core/extend.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ interface CashStatic {
66
}
77

88
interface Cash {
9-
extend ( plugins: plainObject ): this;
9+
extend ( plugins: Record<any, any> ): this;
1010
}
1111

1212
cash.extend = function ( target: any, ...objs: any[] ) {
@@ -27,7 +27,7 @@ cash.extend = function ( target: any, ...objs: any[] ) {
2727

2828
};
2929

30-
fn.extend = function ( plugins: plainObject ) {
30+
fn.extend = function ( plugins: Record<string, any> ) {
3131

3232
return cash.extend ( fn, plugins );
3333

src/core/get_compare_function.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
// @require ./matches.ts
33
// @require ./type_checking.ts
44

5-
function getCompareFunction ( comparator?: Comparator ): (( i: number, ele: Ele ) => boolean) {
5+
function getCompareFunction ( comparator?: Comparator ): (( i: number, ele: EleLoose ) => boolean) {
66

77
return isString ( comparator )
8-
? ( i: number, ele: Ele ) => matches ( ele, comparator )
8+
? ( i: number, ele: EleLoose ) => matches ( ele, comparator )
99
: isFunction ( comparator )
1010
? comparator
1111
: isCash ( comparator )
12-
? ( i: number, ele: Ele ) => comparator.is ( ele )
12+
? ( i: number, ele: EleLoose ) => comparator.is ( ele )
1313
: !comparator
1414
? () => false
15-
: ( i: number, ele: Ele ) => ele === comparator;
15+
: ( i: number, ele: EleLoose ) => ele === comparator;
1616

1717
}

src/core/parse_html.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// @require manipulation/detach.ts
77

88
interface CashStatic {
9-
parseHTML ( html: string ): Ele[];
9+
parseHTML ( html: string ): EleLoose[];
1010
}
1111

1212
const fragmentRe = /^\s*<(\w+)[^>]*>/,
@@ -22,7 +22,7 @@ const containers = {
2222
tfoot: table
2323
};
2424

25-
function parseHTML ( html: string ): Ele[] {
25+
function parseHTML ( html: string ): EleLoose[] {
2626

2727
if ( !isString ( html ) ) return [];
2828

src/core/pluck.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
// @require ./type_checking.ts
33
// @require ./variables.ts
44

5-
type PluckCallback<T> = ( ele: T ) => ArrayLike<T>;
5+
type PluckCallback<T> = ( ele: T ) => ArrayLike<Ele>;
66

7-
function pluck<T> ( arr: ArrayLike<T>, prop: PluckCallback<T> ): Array<T>;
8-
function pluck<T> ( arr: ArrayLike<T>, prop: string, deep?: boolean ): Array<T>;
9-
function pluck<T> ( arr: ArrayLike<T>, prop: string | PluckCallback<T>, deep?: boolean ): Array<T> {
7+
function pluck<T, U extends ArrayLike<T> = ArrayLike<T>> ( arr: U, prop: PluckCallback<U[0]> ): Array<Ele>;
8+
function pluck<T, U extends ArrayLike<T> = ArrayLike<T>> ( arr: U, prop: string, deep?: boolean ): Array<Ele>;
9+
function pluck<T, U extends ArrayLike<T> = ArrayLike<T>> ( arr: U, prop: string | PluckCallback<U[0]>, deep?: boolean ): Array<Ele> {
1010

11-
const plucked: Array<T> = [],
11+
const plucked: Array<Ele> = [],
1212
isCallback = isFunction ( prop );
1313

1414
for ( let i = 0, l = arr.length; i < l; i++ ) {

0 commit comments

Comments
 (0)