Skip to content

Commit d283275

Browse files
committed
Use a different example, and get it in the readonly filter
1 parent 4ac0e1d commit d283275

File tree

43 files changed

+518
-98
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+518
-98
lines changed

src/lib/es5.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,12 @@ interface ReadonlyArray<T> {
12291229
* @param thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
12301230
*/
12311231
filter<S extends T>(predicate: (value: T, index: number, array: readonly T[]) => value is S, thisArg?: any): S[];
1232+
/**
1233+
* Filters out nullish values from the array. Used when `Boolean` is passed as the argument to filter.
1234+
* @param predicate the `Boolean` constructor, which validates the truthiness of the value being mapped over.
1235+
* @param thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
1236+
*/
1237+
filter<S extends T>(predicate: BooleanConstructor, thisArg?: any): NonNullable<S>[];
12321238
/**
12331239
* Returns the elements of an array that meet the condition specified in a callback function.
12341240
* @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.
@@ -1431,6 +1437,12 @@ interface Array<T> {
14311437
* @param thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
14321438
*/
14331439
filter(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): T[];
1440+
/**
1441+
* Filters out nullish values from the array. Used when `Boolean` is passed as the argument to filter.
1442+
* @param predicate the `Boolean` constructor, which validates the truthiness of the value being mapped over.
1443+
* @param thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
1444+
*/
1445+
filter<S extends T>(predicate: BooleanConstructor, thisArg?: any): NonNullable<S>[];
14341446
/**
14351447
* 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.
14361448
* @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.

tests/baselines/reference/arrayFilter.symbols

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ var foo = [
1414
]
1515

1616
foo.filter(x => x.name); //should accepted all possible types not only boolean!
17-
>foo.filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
17+
>foo.filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
1818
>foo : Symbol(foo, Decl(arrayFilter.ts, 0, 3))
19-
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
19+
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
2020
>x : Symbol(x, Decl(arrayFilter.ts, 6, 11))
2121
>x.name : Symbol(name, Decl(arrayFilter.ts, 1, 5))
2222
>x : Symbol(x, Decl(arrayFilter.ts, 6, 11))

tests/baselines/reference/arrayFilter.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ var foo = [
2222

2323
foo.filter(x => x.name); //should accepted all possible types not only boolean!
2424
>foo.filter(x => x.name) : { name: string; }[]
25-
>foo.filter : { <S extends { name: string; }>(predicate: (value: { name: string; }, index: number, array: { name: string; }[]) => value is S, thisArg?: any): S[]; (predicate: (value: { name: string; }, index: number, array: { name: string; }[]) => unknown, thisArg?: any): { name: string; }[]; }
25+
>foo.filter : { <S extends { name: string; }>(predicate: (value: { name: string; }, index: number, array: { name: string; }[]) => value is S, thisArg?: any): S[]; (predicate: (value: { name: string; }, index: number, array: { name: string; }[]) => unknown, thisArg?: any): { name: string; }[]; <S extends { name: string; }>(predicate: BooleanConstructor, thisArg?: any): NonNullable<S>[]; }
2626
>foo : { name: string; }[]
27-
>filter : { <S extends { name: string; }>(predicate: (value: { name: string; }, index: number, array: { name: string; }[]) => value is S, thisArg?: any): S[]; (predicate: (value: { name: string; }, index: number, array: { name: string; }[]) => unknown, thisArg?: any): { name: string; }[]; }
27+
>filter : { <S extends { name: string; }>(predicate: (value: { name: string; }, index: number, array: { name: string; }[]) => value is S, thisArg?: any): S[]; (predicate: (value: { name: string; }, index: number, array: { name: string; }[]) => unknown, thisArg?: any): { name: string; }[]; <S extends { name: string; }>(predicate: BooleanConstructor, thisArg?: any): NonNullable<S>[]; }
2828
>x => x.name : (x: { name: string; }) => string
2929
>x : { name: string; }
3030
>x.name : string
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
//// [arrayFilterBoolean.ts]
22
const mixed = [undefined, "string", null]
3-
const result = mixed.filter(Boolean)
3+
const mixedReadonly: Readonly<typeof mixed> = [undefined, "string", null]
4+
5+
const shouldBeJustStringForMutableArray = mixed.filter(Boolean)
6+
7+
const shouldBeJustStringForReadonlyArray = mixedReadonly.filter(Boolean)
48

59
//// [arrayFilterBoolean.js]
610
"use strict";
711
var mixed = [undefined, "string", null];
8-
var result = mixed.filter(Boolean);
12+
var mixedReadonly = [undefined, "string", null];
13+
var shouldBeJustStringForMutableArray = mixed.filter(Boolean);
14+
var shouldBeJustStringForReadonlyArray = mixedReadonly.filter(Boolean);

tests/baselines/reference/arrayFilterBoolean.symbols

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,23 @@ const mixed = [undefined, "string", null]
33
>mixed : Symbol(mixed, Decl(arrayFilterBoolean.ts, 0, 5))
44
>undefined : Symbol(undefined)
55

6-
const result = mixed.filter(Boolean)
7-
>result : Symbol(result, Decl(arrayFilterBoolean.ts, 1, 5))
8-
>mixed.filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
6+
const mixedReadonly: Readonly<typeof mixed> = [undefined, "string", null]
7+
>mixedReadonly : Symbol(mixedReadonly, Decl(arrayFilterBoolean.ts, 1, 5))
8+
>Readonly : Symbol(Readonly, Decl(lib.es5.d.ts, --, --))
99
>mixed : Symbol(mixed, Decl(arrayFilterBoolean.ts, 0, 5))
10-
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
10+
>undefined : Symbol(undefined)
11+
12+
const shouldBeJustStringForMutableArray = mixed.filter(Boolean)
13+
>shouldBeJustStringForMutableArray : Symbol(shouldBeJustStringForMutableArray, Decl(arrayFilterBoolean.ts, 3, 5))
14+
>mixed.filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
15+
>mixed : Symbol(mixed, Decl(arrayFilterBoolean.ts, 0, 5))
16+
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
17+
>Boolean : Symbol(Boolean, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
18+
19+
const shouldBeJustStringForReadonlyArray = mixedReadonly.filter(Boolean)
20+
>shouldBeJustStringForReadonlyArray : Symbol(shouldBeJustStringForReadonlyArray, Decl(arrayFilterBoolean.ts, 5, 5))
21+
>mixedReadonly.filter : Symbol(ReadonlyArray.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
22+
>mixedReadonly : Symbol(mixedReadonly, Decl(arrayFilterBoolean.ts, 1, 5))
23+
>filter : Symbol(ReadonlyArray.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
1124
>Boolean : Symbol(Boolean, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
1225

tests/baselines/reference/arrayFilterBoolean.types

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,27 @@ const mixed = [undefined, "string", null]
66
>"string" : "string"
77
>null : null
88

9-
const result = mixed.filter(Boolean)
10-
>result : (string | null | undefined)[]
9+
const mixedReadonly: Readonly<typeof mixed> = [undefined, "string", null]
10+
>mixedReadonly : readonly (string | null | undefined)[]
11+
>mixed : (string | null | undefined)[]
12+
>[undefined, "string", null] : (string | null | undefined)[]
13+
>undefined : undefined
14+
>"string" : "string"
15+
>null : null
16+
17+
const shouldBeJustStringForMutableArray = mixed.filter(Boolean)
18+
>shouldBeJustStringForMutableArray : (string | null | undefined)[]
1119
>mixed.filter(Boolean) : (string | null | undefined)[]
12-
>mixed.filter : { <S extends string | null | undefined>(predicate: (value: string | null | undefined, index: number, array: (string | null | undefined)[]) => value is S, thisArg?: any): S[]; (predicate: (value: string | null | undefined, index: number, array: (string | null | undefined)[]) => unknown, thisArg?: any): (string | null | undefined)[]; }
20+
>mixed.filter : { <S extends string | null | undefined>(predicate: (value: string | null | undefined, index: number, array: (string | null | undefined)[]) => value is S, thisArg?: any): S[]; (predicate: (value: string | null | undefined, index: number, array: (string | null | undefined)[]) => unknown, thisArg?: any): (string | null | undefined)[]; <S extends string | null | undefined>(predicate: BooleanConstructor, thisArg?: any): NonNullable<S>[]; }
1321
>mixed : (string | null | undefined)[]
14-
>filter : { <S extends string | null | undefined>(predicate: (value: string | null | undefined, index: number, array: (string | null | undefined)[]) => value is S, thisArg?: any): S[]; (predicate: (value: string | null | undefined, index: number, array: (string | null | undefined)[]) => unknown, thisArg?: any): (string | null | undefined)[]; }
22+
>filter : { <S extends string | null | undefined>(predicate: (value: string | null | undefined, index: number, array: (string | null | undefined)[]) => value is S, thisArg?: any): S[]; (predicate: (value: string | null | undefined, index: number, array: (string | null | undefined)[]) => unknown, thisArg?: any): (string | null | undefined)[]; <S extends string | null | undefined>(predicate: BooleanConstructor, thisArg?: any): NonNullable<S>[]; }
23+
>Boolean : BooleanConstructor
24+
25+
const shouldBeJustStringForReadonlyArray = mixedReadonly.filter(Boolean)
26+
>shouldBeJustStringForReadonlyArray : string[]
27+
>mixedReadonly.filter(Boolean) : string[]
28+
>mixedReadonly.filter : { <S extends string | null | undefined>(predicate: (value: string | null | undefined, index: number, array: readonly (string | null | undefined)[]) => value is S, thisArg?: any): S[]; <S extends string | null | undefined>(predicate: BooleanConstructor, thisArg?: any): NonNullable<S>[]; (predicate: (value: string | null | undefined, index: number, array: readonly (string | null | undefined)[]) => unknown, thisArg?: any): (string | null | undefined)[]; <T>(predicate: BooleanConstructor): NonNullable<T>[]; }
29+
>mixedReadonly : readonly (string | null | undefined)[]
30+
>filter : { <S extends string | null | undefined>(predicate: (value: string | null | undefined, index: number, array: readonly (string | null | undefined)[]) => value is S, thisArg?: any): S[]; <S extends string | null | undefined>(predicate: BooleanConstructor, thisArg?: any): NonNullable<S>[]; (predicate: (value: string | null | undefined, index: number, array: readonly (string | null | undefined)[]) => unknown, thisArg?: any): (string | null | undefined)[]; <T>(predicate: BooleanConstructor): NonNullable<T>[]; }
1531
>Boolean : BooleanConstructor
1632

tests/baselines/reference/booleanFilterAnyArray.symbols

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ var ys: any[];
7070

7171
var ys = realanys.filter(Boolean)
7272
>ys : Symbol(ys, Decl(booleanFilterAnyArray.ts, 16, 3), Decl(booleanFilterAnyArray.ts, 17, 3))
73-
>realanys.filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
73+
>realanys.filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
7474
>realanys : Symbol(realanys, Decl(booleanFilterAnyArray.ts, 15, 11))
75-
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
75+
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
7676
>Boolean : Symbol(Boolean, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
7777

7878
var foo = [{ name: 'x' }]
@@ -86,9 +86,9 @@ var foor: Array<{name: string}>
8686

8787
var foor = foo.filter(x => x.name)
8888
>foor : Symbol(foor, Decl(booleanFilterAnyArray.ts, 20, 3), Decl(booleanFilterAnyArray.ts, 21, 3))
89-
>foo.filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
89+
>foo.filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
9090
>foo : Symbol(foo, Decl(booleanFilterAnyArray.ts, 19, 3))
91-
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
91+
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
9292
>x : Symbol(x, Decl(booleanFilterAnyArray.ts, 21, 22))
9393
>x.name : Symbol(name, Decl(booleanFilterAnyArray.ts, 19, 12))
9494
>x : Symbol(x, Decl(booleanFilterAnyArray.ts, 21, 22))
@@ -100,8 +100,8 @@ var foos: Array<boolean>
100100

101101
var foos = [true, true, false, null].filter((thing): thing is boolean => thing !== null)
102102
>foos : Symbol(foos, Decl(booleanFilterAnyArray.ts, 22, 3), Decl(booleanFilterAnyArray.ts, 23, 3))
103-
>[true, true, false, null].filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
104-
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
103+
>[true, true, false, null].filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
104+
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
105105
>thing : Symbol(thing, Decl(booleanFilterAnyArray.ts, 23, 45))
106106
>thing : Symbol(thing, Decl(booleanFilterAnyArray.ts, 23, 45))
107107
>thing : Symbol(thing, Decl(booleanFilterAnyArray.ts, 23, 45))

tests/baselines/reference/booleanFilterAnyArray.types

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ var ys: any[];
4545
var ys = realanys.filter(Boolean)
4646
>ys : any[]
4747
>realanys.filter(Boolean) : any[]
48-
>realanys.filter : { <S extends any>(predicate: (value: any, index: number, array: any[]) => value is S, thisArg?: any): S[]; (predicate: (value: any, index: number, array: any[]) => unknown, thisArg?: any): any[]; }
48+
>realanys.filter : { <S extends any>(predicate: (value: any, index: number, array: any[]) => value is S, thisArg?: any): S[]; (predicate: (value: any, index: number, array: any[]) => unknown, thisArg?: any): any[]; <S extends any>(predicate: BooleanConstructor, thisArg?: any): NonNullable<S>[]; }
4949
>realanys : any[]
50-
>filter : { <S extends any>(predicate: (value: any, index: number, array: any[]) => value is S, thisArg?: any): S[]; (predicate: (value: any, index: number, array: any[]) => unknown, thisArg?: any): any[]; }
50+
>filter : { <S extends any>(predicate: (value: any, index: number, array: any[]) => value is S, thisArg?: any): S[]; (predicate: (value: any, index: number, array: any[]) => unknown, thisArg?: any): any[]; <S extends any>(predicate: BooleanConstructor, thisArg?: any): NonNullable<S>[]; }
5151
>Boolean : BooleanConstructor
5252

5353
var foo = [{ name: 'x' }]
@@ -64,9 +64,9 @@ var foor: Array<{name: string}>
6464
var foor = foo.filter(x => x.name)
6565
>foor : { name: string; }[]
6666
>foo.filter(x => x.name) : { name: string; }[]
67-
>foo.filter : { <S extends { name: string; }>(predicate: (value: { name: string; }, index: number, array: { name: string; }[]) => value is S, thisArg?: any): S[]; (predicate: (value: { name: string; }, index: number, array: { name: string; }[]) => unknown, thisArg?: any): { name: string; }[]; }
67+
>foo.filter : { <S extends { name: string; }>(predicate: (value: { name: string; }, index: number, array: { name: string; }[]) => value is S, thisArg?: any): S[]; (predicate: (value: { name: string; }, index: number, array: { name: string; }[]) => unknown, thisArg?: any): { name: string; }[]; <S extends { name: string; }>(predicate: BooleanConstructor, thisArg?: any): NonNullable<S>[]; }
6868
>foo : { name: string; }[]
69-
>filter : { <S extends { name: string; }>(predicate: (value: { name: string; }, index: number, array: { name: string; }[]) => value is S, thisArg?: any): S[]; (predicate: (value: { name: string; }, index: number, array: { name: string; }[]) => unknown, thisArg?: any): { name: string; }[]; }
69+
>filter : { <S extends { name: string; }>(predicate: (value: { name: string; }, index: number, array: { name: string; }[]) => value is S, thisArg?: any): S[]; (predicate: (value: { name: string; }, index: number, array: { name: string; }[]) => unknown, thisArg?: any): { name: string; }[]; <S extends { name: string; }>(predicate: BooleanConstructor, thisArg?: any): NonNullable<S>[]; }
7070
>x => x.name : (x: { name: string; }) => string
7171
>x : { name: string; }
7272
>x.name : string
@@ -79,13 +79,13 @@ var foos: Array<boolean>
7979
var foos = [true, true, false, null].filter((thing): thing is boolean => thing !== null)
8080
>foos : boolean[]
8181
>[true, true, false, null].filter((thing): thing is boolean => thing !== null) : boolean[]
82-
>[true, true, false, null].filter : { <S extends boolean>(predicate: (value: boolean, index: number, array: boolean[]) => value is S, thisArg?: any): S[]; (predicate: (value: boolean, index: number, array: boolean[]) => unknown, thisArg?: any): boolean[]; }
82+
>[true, true, false, null].filter : { <S extends boolean>(predicate: (value: boolean, index: number, array: boolean[]) => value is S, thisArg?: any): S[]; (predicate: (value: boolean, index: number, array: boolean[]) => unknown, thisArg?: any): boolean[]; <S extends boolean>(predicate: BooleanConstructor, thisArg?: any): NonNullable<S>[]; }
8383
>[true, true, false, null] : boolean[]
8484
>true : true
8585
>true : true
8686
>false : false
8787
>null : null
88-
>filter : { <S extends boolean>(predicate: (value: boolean, index: number, array: boolean[]) => value is S, thisArg?: any): S[]; (predicate: (value: boolean, index: number, array: boolean[]) => unknown, thisArg?: any): boolean[]; }
88+
>filter : { <S extends boolean>(predicate: (value: boolean, index: number, array: boolean[]) => value is S, thisArg?: any): S[]; (predicate: (value: boolean, index: number, array: boolean[]) => unknown, thisArg?: any): boolean[]; <S extends boolean>(predicate: BooleanConstructor, thisArg?: any): NonNullable<S>[]; }
8989
>(thing): thing is boolean => thing !== null : (thing: boolean) => thing is boolean
9090
>thing : boolean
9191
>thing !== null : boolean

0 commit comments

Comments
 (0)