Skip to content

support type predicate callback functions on Array<T>.filter #11858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,14 @@ interface Array<T> {
map<U>(callbackfn: (this: void, value: T, index: number, array: T[]) => U): U[];
map<U>(callbackfn: (this: void, value: T, index: number, array: T[]) => U, thisArg: undefined): U[];
map<Z, U>(callbackfn: (this: Z, value: T, index: number, array: T[]) => U, thisArg: Z): U[];
/**
* Returns the elements of an array that meet the condition specified in a callback function.
* @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.
* @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.
*/
filter<S extends T>(callbackfn: (this: void, value: T, index: number, array: T[]) => value is S): S[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReadonlyArray and Array should stay in sync. so please update both.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also consider coalescing the overloads using type argument defaults:

filter<S extends T, Z = undefined>(callbackfn: (this: Z, value: T, index: number, array: T[]) => value is S, thisArg?: Z): S[];

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ad first: ReadonlyArray is already updated (see commit comment), so this commit actually brings both classes in sync.

Ad second: I'm not sure if this is the same, as Z is void in the first case and undefined in second. And tt is used the same way 5 lines below the change.

filter<S extends T>(callbackfn: (this: void, value: T, index: number, array: T[]) => value is S, thisArg: undefined): S[];
filter<Z, S extends T>(callbackfn: (this: Z, value: T, index: number, array: T[]) => value is S, thisArg: Z): S[];
/**
* Returns the elements of an array that meet the condition specified in a callback function.
* @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.
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/arrayFilter.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ var foo = [
]

foo.filter(x => x.name); //should accepted all possible types not only boolean!
>foo.filter : Symbol(Array.filter, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>foo.filter : Symbol(Array.filter, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>foo : Symbol(foo, Decl(arrayFilter.ts, 0, 3))
>filter : Symbol(Array.filter, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>filter : Symbol(Array.filter, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(arrayFilter.ts, 6, 11))
>x.name : Symbol(name, Decl(arrayFilter.ts, 1, 5))
>x : Symbol(x, Decl(arrayFilter.ts, 6, 11))
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/arrayFilter.types
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ var foo = [

foo.filter(x => x.name); //should accepted all possible types not only boolean!
>foo.filter(x => x.name) : { name: string; }[]
>foo.filter : { (callbackfn: (this: void, value: { name: string; }, index: number, array: { name: string; }[]) => any): { name: string; }[]; (callbackfn: (this: void, value: { name: string; }, index: number, array: { name: string; }[]) => any, thisArg: undefined): { name: string; }[]; <Z>(callbackfn: (this: Z, value: { name: string; }, index: number, array: { name: string; }[]) => any, thisArg: Z): { name: string; }[]; }
>foo.filter : { <S extends { name: string; }>(callbackfn: (this: void, value: { name: string; }, index: number, array: { name: string; }[]) => value is S): S[]; <S extends { name: string; }>(callbackfn: (this: void, value: { name: string; }, index: number, array: { name: string; }[]) => value is S, thisArg: undefined): S[]; <Z, S extends { name: string; }>(callbackfn: (this: Z, value: { name: string; }, index: number, array: { name: string; }[]) => value is S, thisArg: Z): S[]; (callbackfn: (this: void, value: { name: string; }, index: number, array: { name: string; }[]) => any): { name: string; }[]; (callbackfn: (this: void, value: { name: string; }, index: number, array: { name: string; }[]) => any, thisArg: undefined): { name: string; }[]; <Z>(callbackfn: (this: Z, value: { name: string; }, index: number, array: { name: string; }[]) => any, thisArg: Z): { name: string; }[]; }
>foo : { name: string; }[]
>filter : { (callbackfn: (this: void, value: { name: string; }, index: number, array: { name: string; }[]) => any): { name: string; }[]; (callbackfn: (this: void, value: { name: string; }, index: number, array: { name: string; }[]) => any, thisArg: undefined): { name: string; }[]; <Z>(callbackfn: (this: Z, value: { name: string; }, index: number, array: { name: string; }[]) => any, thisArg: Z): { name: string; }[]; }
>filter : { <S extends { name: string; }>(callbackfn: (this: void, value: { name: string; }, index: number, array: { name: string; }[]) => value is S): S[]; <S extends { name: string; }>(callbackfn: (this: void, value: { name: string; }, index: number, array: { name: string; }[]) => value is S, thisArg: undefined): S[]; <Z, S extends { name: string; }>(callbackfn: (this: Z, value: { name: string; }, index: number, array: { name: string; }[]) => value is S, thisArg: Z): S[]; (callbackfn: (this: void, value: { name: string; }, index: number, array: { name: string; }[]) => any): { name: string; }[]; (callbackfn: (this: void, value: { name: string; }, index: number, array: { name: string; }[]) => any, thisArg: undefined): { name: string; }[]; <Z>(callbackfn: (this: Z, value: { name: string; }, index: number, array: { name: string; }[]) => any, thisArg: Z): { name: string; }[]; }
>x => x.name : (this: void, x: { name: string; }) => string
>x : { name: string; }
>x.name : string
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/declarationEmitPromise.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ export async function runSampleWorks<A, B, C, D, E>(
>bluebird.all : Symbol(bluebird.all, Decl(declarationEmitPromise.ts, 0, 26))
>bluebird : Symbol(bluebird, Decl(declarationEmitPromise.ts, 0, 0))
>all : Symbol(bluebird.all, Decl(declarationEmitPromise.ts, 0, 26))
>[a, b, c, d, e].filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>[a, b, c, d, e].filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>a : Symbol(a, Decl(declarationEmitPromise.ts, 4, 52))
>b : Symbol(b, Decl(declarationEmitPromise.ts, 5, 19))
>c : Symbol(c, Decl(declarationEmitPromise.ts, 5, 36))
>d : Symbol(d, Decl(declarationEmitPromise.ts, 5, 53))
>e : Symbol(e, Decl(declarationEmitPromise.ts, 5, 70))
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>el : Symbol(el, Decl(declarationEmitPromise.ts, 6, 68))
>el : Symbol(el, Decl(declarationEmitPromise.ts, 6, 68))

Expand Down Expand Up @@ -111,13 +111,13 @@ export async function runSampleBreaks<A, B, C, D, E>(
>bluebird.all : Symbol(bluebird.all, Decl(declarationEmitPromise.ts, 0, 26))
>bluebird : Symbol(bluebird, Decl(declarationEmitPromise.ts, 0, 0))
>all : Symbol(bluebird.all, Decl(declarationEmitPromise.ts, 0, 26))
>[a, b, c, d, e].filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>[a, b, c, d, e].filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>a : Symbol(a, Decl(declarationEmitPromise.ts, 13, 53))
>b : Symbol(b, Decl(declarationEmitPromise.ts, 14, 19))
>c : Symbol(c, Decl(declarationEmitPromise.ts, 14, 36))
>d : Symbol(d, Decl(declarationEmitPromise.ts, 14, 53))
>e : Symbol(e, Decl(declarationEmitPromise.ts, 14, 70))
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>el : Symbol(el, Decl(declarationEmitPromise.ts, 15, 68))
>el : Symbol(el, Decl(declarationEmitPromise.ts, 15, 68))

Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/declarationEmitPromise.types
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ export async function runSampleWorks<A, B, C, D, E>(
>bluebird : typeof bluebird
>all : bluebird<any>[]
>[a, b, c, d, e].filter(el => !!el) : bluebird<A>[]
>[a, b, c, d, e].filter : { (callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => any): bluebird<A>[]; (callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => any, thisArg: undefined): bluebird<A>[]; <Z>(callbackfn: (this: Z, value: bluebird<A>, index: number, array: bluebird<A>[]) => any, thisArg: Z): bluebird<A>[]; }
>[a, b, c, d, e].filter : { <S extends bluebird<A>>(callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => value is S): S[]; <S extends bluebird<A>>(callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => value is S, thisArg: undefined): S[]; <Z, S extends bluebird<A>>(callbackfn: (this: Z, value: bluebird<A>, index: number, array: bluebird<A>[]) => value is S, thisArg: Z): S[]; (callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => any): bluebird<A>[]; (callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => any, thisArg: undefined): bluebird<A>[]; <Z>(callbackfn: (this: Z, value: bluebird<A>, index: number, array: bluebird<A>[]) => any, thisArg: Z): bluebird<A>[]; }
>[a, b, c, d, e] : bluebird<A>[]
>a : bluebird<A>
>b : bluebird<B>
>c : bluebird<C>
>d : bluebird<D>
>e : bluebird<E>
>filter : { (callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => any): bluebird<A>[]; (callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => any, thisArg: undefined): bluebird<A>[]; <Z>(callbackfn: (this: Z, value: bluebird<A>, index: number, array: bluebird<A>[]) => any, thisArg: Z): bluebird<A>[]; }
>filter : { <S extends bluebird<A>>(callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => value is S): S[]; <S extends bluebird<A>>(callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => value is S, thisArg: undefined): S[]; <Z, S extends bluebird<A>>(callbackfn: (this: Z, value: bluebird<A>, index: number, array: bluebird<A>[]) => value is S, thisArg: Z): S[]; (callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => any): bluebird<A>[]; (callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => any, thisArg: undefined): bluebird<A>[]; <Z>(callbackfn: (this: Z, value: bluebird<A>, index: number, array: bluebird<A>[]) => any, thisArg: Z): bluebird<A>[]; }
>el => !!el : (this: void, el: bluebird<A>) => boolean
>el : bluebird<A>
>!!el : boolean
Expand Down Expand Up @@ -129,14 +129,14 @@ export async function runSampleBreaks<A, B, C, D, E>(
>bluebird : typeof bluebird
>all : bluebird<any>[]
>[a, b, c, d, e].filter(el => !!el) : bluebird<A>[]
>[a, b, c, d, e].filter : { (callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => any): bluebird<A>[]; (callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => any, thisArg: undefined): bluebird<A>[]; <Z>(callbackfn: (this: Z, value: bluebird<A>, index: number, array: bluebird<A>[]) => any, thisArg: Z): bluebird<A>[]; }
>[a, b, c, d, e].filter : { <S extends bluebird<A>>(callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => value is S): S[]; <S extends bluebird<A>>(callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => value is S, thisArg: undefined): S[]; <Z, S extends bluebird<A>>(callbackfn: (this: Z, value: bluebird<A>, index: number, array: bluebird<A>[]) => value is S, thisArg: Z): S[]; (callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => any): bluebird<A>[]; (callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => any, thisArg: undefined): bluebird<A>[]; <Z>(callbackfn: (this: Z, value: bluebird<A>, index: number, array: bluebird<A>[]) => any, thisArg: Z): bluebird<A>[]; }
>[a, b, c, d, e] : bluebird<A>[]
>a : bluebird<A>
>b : bluebird<B>
>c : bluebird<C>
>d : bluebird<D>
>e : bluebird<E>
>filter : { (callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => any): bluebird<A>[]; (callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => any, thisArg: undefined): bluebird<A>[]; <Z>(callbackfn: (this: Z, value: bluebird<A>, index: number, array: bluebird<A>[]) => any, thisArg: Z): bluebird<A>[]; }
>filter : { <S extends bluebird<A>>(callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => value is S): S[]; <S extends bluebird<A>>(callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => value is S, thisArg: undefined): S[]; <Z, S extends bluebird<A>>(callbackfn: (this: Z, value: bluebird<A>, index: number, array: bluebird<A>[]) => value is S, thisArg: Z): S[]; (callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => any): bluebird<A>[]; (callbackfn: (this: void, value: bluebird<A>, index: number, array: bluebird<A>[]) => any, thisArg: undefined): bluebird<A>[]; <Z>(callbackfn: (this: Z, value: bluebird<A>, index: number, array: bluebird<A>[]) => any, thisArg: Z): bluebird<A>[]; }
>el => !!el : (this: void, el: bluebird<A>) => boolean
>el : bluebird<A>
>!!el : boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ var elements = names.map(function (name) {

var xxx = elements.filter(function (e) {
>xxx : Symbol(xxx, Decl(genericMethodOverspecialization.ts, 17, 3))
>elements.filter : Symbol(Array.filter, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>elements.filter : Symbol(Array.filter, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>elements : Symbol(elements, Decl(genericMethodOverspecialization.ts, 12, 3))
>filter : Symbol(Array.filter, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>filter : Symbol(Array.filter, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>e : Symbol(e, Decl(genericMethodOverspecialization.ts, 17, 36))

return !e.isDisabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ var elements = names.map(function (name) {
var xxx = elements.filter(function (e) {
>xxx : HTMLElement[]
>elements.filter(function (e) { return !e.isDisabled;}) : HTMLElement[]
>elements.filter : { (callbackfn: (this: void, value: HTMLElement, index: number, array: HTMLElement[]) => any): HTMLElement[]; (callbackfn: (this: void, value: HTMLElement, index: number, array: HTMLElement[]) => any, thisArg: undefined): HTMLElement[]; <Z>(callbackfn: (this: Z, value: HTMLElement, index: number, array: HTMLElement[]) => any, thisArg: Z): HTMLElement[]; }
>elements.filter : { <S extends HTMLElement>(callbackfn: (this: void, value: HTMLElement, index: number, array: HTMLElement[]) => value is S): S[]; <S extends HTMLElement>(callbackfn: (this: void, value: HTMLElement, index: number, array: HTMLElement[]) => value is S, thisArg: undefined): S[]; <Z, S extends HTMLElement>(callbackfn: (this: Z, value: HTMLElement, index: number, array: HTMLElement[]) => value is S, thisArg: Z): S[]; (callbackfn: (this: void, value: HTMLElement, index: number, array: HTMLElement[]) => any): HTMLElement[]; (callbackfn: (this: void, value: HTMLElement, index: number, array: HTMLElement[]) => any, thisArg: undefined): HTMLElement[]; <Z>(callbackfn: (this: Z, value: HTMLElement, index: number, array: HTMLElement[]) => any, thisArg: Z): HTMLElement[]; }
>elements : HTMLElement[]
>filter : { (callbackfn: (this: void, value: HTMLElement, index: number, array: HTMLElement[]) => any): HTMLElement[]; (callbackfn: (this: void, value: HTMLElement, index: number, array: HTMLElement[]) => any, thisArg: undefined): HTMLElement[]; <Z>(callbackfn: (this: Z, value: HTMLElement, index: number, array: HTMLElement[]) => any, thisArg: Z): HTMLElement[]; }
>filter : { <S extends HTMLElement>(callbackfn: (this: void, value: HTMLElement, index: number, array: HTMLElement[]) => value is S): S[]; <S extends HTMLElement>(callbackfn: (this: void, value: HTMLElement, index: number, array: HTMLElement[]) => value is S, thisArg: undefined): S[]; <Z, S extends HTMLElement>(callbackfn: (this: Z, value: HTMLElement, index: number, array: HTMLElement[]) => value is S, thisArg: Z): S[]; (callbackfn: (this: void, value: HTMLElement, index: number, array: HTMLElement[]) => any): HTMLElement[]; (callbackfn: (this: void, value: HTMLElement, index: number, array: HTMLElement[]) => any, thisArg: undefined): HTMLElement[]; <Z>(callbackfn: (this: Z, value: HTMLElement, index: number, array: HTMLElement[]) => any, thisArg: Z): HTMLElement[]; }
>function (e) { return !e.isDisabled;} : (this: void, e: HTMLElement) => boolean
>e : HTMLElement

Expand Down