Skip to content

Commit 3e2fff2

Browse files
NimaZahediyuit
authored andcommitted
Array.prototype.filter.not.forcing.boolean (#7779)
* Add test for issue * Fix issue * Add baselines * fix issue
1 parent 0a2a8cc commit 3e2fff2

File tree

6 files changed

+93
-13
lines changed

6 files changed

+93
-13
lines changed

src/lib/es5.d.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ interface ReadonlyArray<T> {
10641064
* @param callbackfn A function that accepts up to three arguments. The filter method calls the callbackfn function one time for each element in the array.
10651065
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
10661066
*/
1067-
filter(callbackfn: (value: T, index: number, array: ReadonlyArray<T>) => boolean, thisArg?: any): T[];
1067+
filter(callbackfn: (value: T, index: number, array: ReadonlyArray<T>) => any, thisArg?: any): T[];
10681068
/**
10691069
* Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
10701070
* @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
@@ -1199,7 +1199,7 @@ interface Array<T> {
11991199
* @param callbackfn A function that accepts up to three arguments. The filter method calls the callbackfn function one time for each element in the array.
12001200
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
12011201
*/
1202-
filter(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): T[];
1202+
filter(callbackfn: (value: T, index: number, array: T[]) => any, thisArg?: any): T[];
12031203
/**
12041204
* Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
12051205
* @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
@@ -1511,7 +1511,7 @@ interface Int8Array {
15111511
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
15121512
* If thisArg is omitted, undefined is used as the this value.
15131513
*/
1514-
filter(callbackfn: (value: number, index: number, array: Int8Array) => boolean, thisArg?: any): Int8Array;
1514+
filter(callbackfn: (value: number, index: number, array: Int8Array) => any, thisArg?: any): Int8Array;
15151515

15161516
/**
15171517
* Returns the value of the first element in the array where predicate is true, and undefined
@@ -1784,7 +1784,7 @@ interface Uint8Array {
17841784
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
17851785
* If thisArg is omitted, undefined is used as the this value.
17861786
*/
1787-
filter(callbackfn: (value: number, index: number, array: Uint8Array) => boolean, thisArg?: any): Uint8Array;
1787+
filter(callbackfn: (value: number, index: number, array: Uint8Array) => any, thisArg?: any): Uint8Array;
17881788

17891789
/**
17901790
* Returns the value of the first element in the array where predicate is true, and undefined
@@ -2058,7 +2058,7 @@ interface Uint8ClampedArray {
20582058
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
20592059
* If thisArg is omitted, undefined is used as the this value.
20602060
*/
2061-
filter(callbackfn: (value: number, index: number, array: Uint8ClampedArray) => boolean, thisArg?: any): Uint8ClampedArray;
2061+
filter(callbackfn: (value: number, index: number, array: Uint8ClampedArray) => any, thisArg?: any): Uint8ClampedArray;
20622062

20632063
/**
20642064
* Returns the value of the first element in the array where predicate is true, and undefined
@@ -2331,7 +2331,7 @@ interface Int16Array {
23312331
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
23322332
* If thisArg is omitted, undefined is used as the this value.
23332333
*/
2334-
filter(callbackfn: (value: number, index: number, array: Int16Array) => boolean, thisArg?: any): Int16Array;
2334+
filter(callbackfn: (value: number, index: number, array: Int16Array) => any, thisArg?: any): Int16Array;
23352335

23362336
/**
23372337
* Returns the value of the first element in the array where predicate is true, and undefined
@@ -2605,7 +2605,7 @@ interface Uint16Array {
26052605
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
26062606
* If thisArg is omitted, undefined is used as the this value.
26072607
*/
2608-
filter(callbackfn: (value: number, index: number, array: Uint16Array) => boolean, thisArg?: any): Uint16Array;
2608+
filter(callbackfn: (value: number, index: number, array: Uint16Array) => any, thisArg?: any): Uint16Array;
26092609

26102610
/**
26112611
* Returns the value of the first element in the array where predicate is true, and undefined
@@ -2878,7 +2878,7 @@ interface Int32Array {
28782878
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
28792879
* If thisArg is omitted, undefined is used as the this value.
28802880
*/
2881-
filter(callbackfn: (value: number, index: number, array: Int32Array) => boolean, thisArg?: any): Int32Array;
2881+
filter(callbackfn: (value: number, index: number, array: Int32Array) => any, thisArg?: any): Int32Array;
28822882

28832883
/**
28842884
* Returns the value of the first element in the array where predicate is true, and undefined
@@ -3151,7 +3151,7 @@ interface Uint32Array {
31513151
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
31523152
* If thisArg is omitted, undefined is used as the this value.
31533153
*/
3154-
filter(callbackfn: (value: number, index: number, array: Uint32Array) => boolean, thisArg?: any): Uint32Array;
3154+
filter(callbackfn: (value: number, index: number, array: Uint32Array) => any, thisArg?: any): Uint32Array;
31553155

31563156
/**
31573157
* Returns the value of the first element in the array where predicate is true, and undefined
@@ -3424,7 +3424,7 @@ interface Float32Array {
34243424
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
34253425
* If thisArg is omitted, undefined is used as the this value.
34263426
*/
3427-
filter(callbackfn: (value: number, index: number, array: Float32Array) => boolean, thisArg?: any): Float32Array;
3427+
filter(callbackfn: (value: number, index: number, array: Float32Array) => any, thisArg?: any): Float32Array;
34283428

34293429
/**
34303430
* Returns the value of the first element in the array where predicate is true, and undefined
@@ -3698,7 +3698,7 @@ interface Float64Array {
36983698
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
36993699
* If thisArg is omitted, undefined is used as the this value.
37003700
*/
3701-
filter(callbackfn: (value: number, index: number, array: Float64Array) => boolean, thisArg?: any): Float64Array;
3701+
filter(callbackfn: (value: number, index: number, array: Float64Array) => any, thisArg?: any): Float64Array;
37023702

37033703
/**
37043704
* Returns the value of the first element in the array where predicate is true, and undefined
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [arrayFilter.ts]
2+
var foo = [
3+
{ name: 'bar' },
4+
{ name: null },
5+
{ name: 'baz' }
6+
]
7+
8+
foo.filter(x => x.name); //should accepted all possible types not only boolean!
9+
10+
//// [arrayFilter.js]
11+
var foo = [
12+
{ name: 'bar' },
13+
{ name: null },
14+
{ name: 'baz' }
15+
];
16+
foo.filter(function (x) { return x.name; }); //should accepted all possible types not only boolean!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/compiler/arrayFilter.ts ===
2+
var foo = [
3+
>foo : Symbol(foo, Decl(arrayFilter.ts, 0, 3))
4+
5+
{ name: 'bar' },
6+
>name : Symbol(name, Decl(arrayFilter.ts, 1, 5))
7+
8+
{ name: null },
9+
>name : Symbol(name, Decl(arrayFilter.ts, 2, 5))
10+
11+
{ name: 'baz' }
12+
>name : Symbol(name, Decl(arrayFilter.ts, 3, 5))
13+
14+
]
15+
16+
foo.filter(x => x.name); //should accepted all possible types not only boolean!
17+
>foo.filter : Symbol(Array.filter, Decl(lib.d.ts, --, --))
18+
>foo : Symbol(foo, Decl(arrayFilter.ts, 0, 3))
19+
>filter : Symbol(Array.filter, Decl(lib.d.ts, --, --))
20+
>x : Symbol(x, Decl(arrayFilter.ts, 6, 11))
21+
>x.name : Symbol(name, Decl(arrayFilter.ts, 1, 5))
22+
>x : Symbol(x, Decl(arrayFilter.ts, 6, 11))
23+
>name : Symbol(name, Decl(arrayFilter.ts, 1, 5))
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
=== tests/cases/compiler/arrayFilter.ts ===
2+
var foo = [
3+
>foo : { name: string; }[]
4+
>[ { name: 'bar' }, { name: null }, { name: 'baz' }] : { name: string; }[]
5+
6+
{ name: 'bar' },
7+
>{ name: 'bar' } : { name: string; }
8+
>name : string
9+
>'bar' : string
10+
11+
{ name: null },
12+
>{ name: null } : { name: null; }
13+
>name : null
14+
>null : null
15+
16+
{ name: 'baz' }
17+
>{ name: 'baz' } : { name: string; }
18+
>name : string
19+
>'baz' : string
20+
21+
]
22+
23+
foo.filter(x => x.name); //should accepted all possible types not only boolean!
24+
>foo.filter(x => x.name) : { name: string; }[]
25+
>foo.filter : (callbackfn: (value: { name: string; }, index: number, array: { name: string; }[]) => any, thisArg?: any) => { name: string; }[]
26+
>foo : { name: string; }[]
27+
>filter : (callbackfn: (value: { name: string; }, index: number, array: { name: string; }[]) => any, thisArg?: any) => { name: string; }[]
28+
>x => x.name : (x: { name: string; }) => string
29+
>x : { name: string; }
30+
>x.name : string
31+
>x : { name: string; }
32+
>name : string
33+

tests/baselines/reference/genericMethodOverspecialization.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ var elements = names.map(function (name) {
5353
var xxx = elements.filter(function (e) {
5454
>xxx : HTMLElement[]
5555
>elements.filter(function (e) { return !e.isDisabled;}) : HTMLElement[]
56-
>elements.filter : (callbackfn: (value: HTMLElement, index: number, array: HTMLElement[]) => boolean, thisArg?: any) => HTMLElement[]
56+
>elements.filter : (callbackfn: (value: HTMLElement, index: number, array: HTMLElement[]) => any, thisArg?: any) => HTMLElement[]
5757
>elements : HTMLElement[]
58-
>filter : (callbackfn: (value: HTMLElement, index: number, array: HTMLElement[]) => boolean, thisArg?: any) => HTMLElement[]
58+
>filter : (callbackfn: (value: HTMLElement, index: number, array: HTMLElement[]) => any, thisArg?: any) => HTMLElement[]
5959
>function (e) { return !e.isDisabled;} : (e: HTMLElement) => boolean
6060
>e : HTMLElement
6161

tests/cases/compiler/arrayFilter.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var foo = [
2+
{ name: 'bar' },
3+
{ name: null },
4+
{ name: 'baz' }
5+
]
6+
7+
foo.filter(x => x.name); //should accepted all possible types not only boolean!

0 commit comments

Comments
 (0)