Skip to content

Make anyArray.filter(Boolean) return any[], not unknown[] #31515

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

Merged
merged 5 commits into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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: 7 additions & 1 deletion src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ interface Boolean {

interface BooleanConstructor {
new(value?: any): Boolean;
<T>(value?: T): value is Exclude<T, false | null | undefined | '' | 0>;
<T extends any>(value?: T): value is Exclude<T, false | null | undefined | '' | 0>;
Copy link
Member

Choose a reason for hiding this comment

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

#29571 will 100% break this (and we only held off merging it because we got spooked with the number of breaks already in 3.5 IIRC), since this is a terrible hack that makes T "look like any" even though it's a type parameter and extends any should be identical to extends unknown or simply no constraint.

readonly prototype: Boolean;
}

Expand Down Expand Up @@ -1133,6 +1133,12 @@ interface ReadonlyArray<T> {
* @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.
*/
map<U>(callbackfn: (value: T, index: number, array: ReadonlyArray<T>) => U, thisArg?: any): 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(this: ReadonlyArray<never>, callbackfn: (value: any, index: number, array: ReadonlyArray<any>) => unknown, thisArg?: any): any[];
/**
* 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
37 changes: 37 additions & 0 deletions tests/baselines/reference/booleanFilterAnyArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//// [booleanFilterAnyArray.ts]
interface Bullean { }
interface BulleanConstructor {
new(v1?: any): Bullean;
<T>(v2?: T): v2 is T;
}

interface Ari<T> {
filter<S extends T>(cb1: (value: T) => value is S): T extends any ? Ari<any> : Ari<S>;
filter(cb2: (value: T) => unknown): Ari<T>;
}
declare var Bullean: BulleanConstructor;
declare let anys: Ari<any>;
var xs: Ari<any>;
var xs = anys.filter(Bullean)

declare let realanys: any[];
var ys: any[];
var ys = realanys.filter(Boolean)

var foo = [{ name: 'x' }]
var foor: Array<{name: string}>
var foor = foo.filter(x => x.name)
var foos: Array<boolean>
var foos = [true, true, false, null].filter((thing): thing is boolean => thing !== null)


//// [booleanFilterAnyArray.js]
var xs;
var xs = anys.filter(Bullean);
var ys;
var ys = realanys.filter(Boolean);
var foo = [{ name: 'x' }];
var foor;
var foor = foo.filter(function (x) { return x.name; });
var foos;
var foos = [true, true, false, null].filter(function (thing) { return thing !== null; });
108 changes: 108 additions & 0 deletions tests/baselines/reference/booleanFilterAnyArray.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
=== tests/cases/compiler/booleanFilterAnyArray.ts ===
interface Bullean { }
>Bullean : Symbol(Bullean, Decl(booleanFilterAnyArray.ts, 0, 0), Decl(booleanFilterAnyArray.ts, 10, 11))

interface BulleanConstructor {
>BulleanConstructor : Symbol(BulleanConstructor, Decl(booleanFilterAnyArray.ts, 0, 21))

new(v1?: any): Bullean;
>v1 : Symbol(v1, Decl(booleanFilterAnyArray.ts, 2, 8))
>Bullean : Symbol(Bullean, Decl(booleanFilterAnyArray.ts, 0, 0), Decl(booleanFilterAnyArray.ts, 10, 11))

<T>(v2?: T): v2 is T;
>T : Symbol(T, Decl(booleanFilterAnyArray.ts, 3, 5))
>v2 : Symbol(v2, Decl(booleanFilterAnyArray.ts, 3, 8))
>T : Symbol(T, Decl(booleanFilterAnyArray.ts, 3, 5))
>v2 : Symbol(v2, Decl(booleanFilterAnyArray.ts, 3, 8))
>T : Symbol(T, Decl(booleanFilterAnyArray.ts, 3, 5))
}

interface Ari<T> {
>Ari : Symbol(Ari, Decl(booleanFilterAnyArray.ts, 4, 1))
>T : Symbol(T, Decl(booleanFilterAnyArray.ts, 6, 14))

filter<S extends T>(cb1: (value: T) => value is S): T extends any ? Ari<any> : Ari<S>;
>filter : Symbol(Ari.filter, Decl(booleanFilterAnyArray.ts, 6, 18), Decl(booleanFilterAnyArray.ts, 7, 90))
>S : Symbol(S, Decl(booleanFilterAnyArray.ts, 7, 11))
>T : Symbol(T, Decl(booleanFilterAnyArray.ts, 6, 14))
>cb1 : Symbol(cb1, Decl(booleanFilterAnyArray.ts, 7, 24))
>value : Symbol(value, Decl(booleanFilterAnyArray.ts, 7, 30))
>T : Symbol(T, Decl(booleanFilterAnyArray.ts, 6, 14))
>value : Symbol(value, Decl(booleanFilterAnyArray.ts, 7, 30))
>S : Symbol(S, Decl(booleanFilterAnyArray.ts, 7, 11))
>T : Symbol(T, Decl(booleanFilterAnyArray.ts, 6, 14))
>Ari : Symbol(Ari, Decl(booleanFilterAnyArray.ts, 4, 1))
>Ari : Symbol(Ari, Decl(booleanFilterAnyArray.ts, 4, 1))
>S : Symbol(S, Decl(booleanFilterAnyArray.ts, 7, 11))

filter(cb2: (value: T) => unknown): Ari<T>;
>filter : Symbol(Ari.filter, Decl(booleanFilterAnyArray.ts, 6, 18), Decl(booleanFilterAnyArray.ts, 7, 90))
>cb2 : Symbol(cb2, Decl(booleanFilterAnyArray.ts, 8, 11))
>value : Symbol(value, Decl(booleanFilterAnyArray.ts, 8, 17))
>T : Symbol(T, Decl(booleanFilterAnyArray.ts, 6, 14))
>Ari : Symbol(Ari, Decl(booleanFilterAnyArray.ts, 4, 1))
>T : Symbol(T, Decl(booleanFilterAnyArray.ts, 6, 14))
}
declare var Bullean: BulleanConstructor;
>Bullean : Symbol(Bullean, Decl(booleanFilterAnyArray.ts, 0, 0), Decl(booleanFilterAnyArray.ts, 10, 11))
>BulleanConstructor : Symbol(BulleanConstructor, Decl(booleanFilterAnyArray.ts, 0, 21))

declare let anys: Ari<any>;
>anys : Symbol(anys, Decl(booleanFilterAnyArray.ts, 11, 11))
>Ari : Symbol(Ari, Decl(booleanFilterAnyArray.ts, 4, 1))

var xs: Ari<any>;
>xs : Symbol(xs, Decl(booleanFilterAnyArray.ts, 12, 3), Decl(booleanFilterAnyArray.ts, 13, 3))
>Ari : Symbol(Ari, Decl(booleanFilterAnyArray.ts, 4, 1))

var xs = anys.filter(Bullean)
>xs : Symbol(xs, Decl(booleanFilterAnyArray.ts, 12, 3), Decl(booleanFilterAnyArray.ts, 13, 3))
>anys.filter : Symbol(Ari.filter, Decl(booleanFilterAnyArray.ts, 6, 18), Decl(booleanFilterAnyArray.ts, 7, 90))
>anys : Symbol(anys, Decl(booleanFilterAnyArray.ts, 11, 11))
>filter : Symbol(Ari.filter, Decl(booleanFilterAnyArray.ts, 6, 18), Decl(booleanFilterAnyArray.ts, 7, 90))
>Bullean : Symbol(Bullean, Decl(booleanFilterAnyArray.ts, 0, 0), Decl(booleanFilterAnyArray.ts, 10, 11))

declare let realanys: any[];
>realanys : Symbol(realanys, Decl(booleanFilterAnyArray.ts, 15, 11))

var ys: any[];
>ys : Symbol(ys, Decl(booleanFilterAnyArray.ts, 16, 3), Decl(booleanFilterAnyArray.ts, 17, 3))

var ys = realanys.filter(Boolean)
>ys : Symbol(ys, Decl(booleanFilterAnyArray.ts, 16, 3), Decl(booleanFilterAnyArray.ts, 17, 3))
>realanys.filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>realanys : Symbol(realanys, Decl(booleanFilterAnyArray.ts, 15, 11))
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Boolean : Symbol(Boolean, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

var foo = [{ name: 'x' }]
>foo : Symbol(foo, Decl(booleanFilterAnyArray.ts, 19, 3))
>name : Symbol(name, Decl(booleanFilterAnyArray.ts, 19, 12))

var foor: Array<{name: string}>
>foor : Symbol(foor, Decl(booleanFilterAnyArray.ts, 20, 3), Decl(booleanFilterAnyArray.ts, 21, 3))
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>name : Symbol(name, Decl(booleanFilterAnyArray.ts, 20, 17))

var foor = foo.filter(x => x.name)
>foor : Symbol(foor, Decl(booleanFilterAnyArray.ts, 20, 3), Decl(booleanFilterAnyArray.ts, 21, 3))
>foo.filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>foo : Symbol(foo, Decl(booleanFilterAnyArray.ts, 19, 3))
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>x : Symbol(x, Decl(booleanFilterAnyArray.ts, 21, 22))
>x.name : Symbol(name, Decl(booleanFilterAnyArray.ts, 19, 12))
>x : Symbol(x, Decl(booleanFilterAnyArray.ts, 21, 22))
>name : Symbol(name, Decl(booleanFilterAnyArray.ts, 19, 12))

var foos: Array<boolean>
>foos : Symbol(foos, Decl(booleanFilterAnyArray.ts, 22, 3), Decl(booleanFilterAnyArray.ts, 23, 3))
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

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

94 changes: 94 additions & 0 deletions tests/baselines/reference/booleanFilterAnyArray.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
=== tests/cases/compiler/booleanFilterAnyArray.ts ===
interface Bullean { }
interface BulleanConstructor {
new(v1?: any): Bullean;
>v1 : any

<T>(v2?: T): v2 is T;
>v2 : T
}

interface Ari<T> {
filter<S extends T>(cb1: (value: T) => value is S): T extends any ? Ari<any> : Ari<S>;
>filter : { <S extends T>(cb1: (value: T) => value is S): T extends any ? Ari<any> : Ari<S>; (cb2: (value: T) => unknown): Ari<T>; }
>cb1 : (value: T) => value is S
>value : T

filter(cb2: (value: T) => unknown): Ari<T>;
>filter : { <S extends T>(cb1: (value: T) => value is S): T extends any ? Ari<any> : Ari<S>; (cb2: (value: T) => unknown): Ari<T>; }
>cb2 : (value: T) => unknown
>value : T
}
declare var Bullean: BulleanConstructor;
>Bullean : BulleanConstructor

declare let anys: Ari<any>;
>anys : Ari<any>

var xs: Ari<any>;
>xs : Ari<any>

var xs = anys.filter(Bullean)
>xs : Ari<any>
>anys.filter(Bullean) : Ari<any>
>anys.filter : { <S extends any>(cb1: (value: any) => value is S): Ari<any>; (cb2: (value: any) => unknown): Ari<any>; }
>anys : Ari<any>
>filter : { <S extends any>(cb1: (value: any) => value is S): Ari<any>; (cb2: (value: any) => unknown): Ari<any>; }
>Bullean : BulleanConstructor

declare let realanys: any[];
>realanys : any[]

var ys: any[];
>ys : any[]

var ys = realanys.filter(Boolean)
>ys : any[]
>realanys.filter(Boolean) : any[]
>realanys.filter : { <S extends any>(callbackfn: (value: any, index: number, array: any[]) => value is S, thisArg?: any): S[]; (callbackfn: (value: any, index: number, array: any[]) => unknown, thisArg?: any): any[]; }
>realanys : any[]
>filter : { <S extends any>(callbackfn: (value: any, index: number, array: any[]) => value is S, thisArg?: any): S[]; (callbackfn: (value: any, index: number, array: any[]) => unknown, thisArg?: any): any[]; }
>Boolean : BooleanConstructor

var foo = [{ name: 'x' }]
>foo : { name: string; }[]
>[{ name: 'x' }] : { name: string; }[]
>{ name: 'x' } : { name: string; }
>name : string
>'x' : "x"

var foor: Array<{name: string}>
>foor : { name: string; }[]
>name : string

var foor = foo.filter(x => x.name)
>foor : { name: string; }[]
>foo.filter(x => x.name) : { name: string; }[]
>foo.filter : { <S extends { name: string; }>(callbackfn: (value: { name: string; }, index: number, array: { name: string; }[]) => value is S, thisArg?: any): S[]; (callbackfn: (value: { name: string; }, index: number, array: { name: string; }[]) => unknown, thisArg?: any): { name: string; }[]; }
>foo : { name: string; }[]
>filter : { <S extends { name: string; }>(callbackfn: (value: { name: string; }, index: number, array: { name: string; }[]) => value is S, thisArg?: any): S[]; (callbackfn: (value: { name: string; }, index: number, array: { name: string; }[]) => unknown, thisArg?: any): { name: string; }[]; }
>x => x.name : (x: { name: string; }) => string
>x : { name: string; }
>x.name : string
>x : { name: string; }
>name : string

var foos: Array<boolean>
>foos : boolean[]

var foos = [true, true, false, null].filter((thing): thing is boolean => thing !== null)
>foos : boolean[]
>[true, true, false, null].filter((thing): thing is boolean => thing !== null) : boolean[]
>[true, true, false, null].filter : { <S extends boolean>(callbackfn: (value: boolean, index: number, array: boolean[]) => value is S, thisArg?: any): S[]; (callbackfn: (value: boolean, index: number, array: boolean[]) => unknown, thisArg?: any): boolean[]; }
>[true, true, false, null] : boolean[]
>true : true
>true : true
>false : false
>null : null
>filter : { <S extends boolean>(callbackfn: (value: boolean, index: number, array: boolean[]) => value is S, thisArg?: any): S[]; (callbackfn: (value: boolean, index: number, array: boolean[]) => unknown, thisArg?: any): boolean[]; }
>(thing): thing is boolean => thing !== null : (thing: boolean) => thing is boolean
>thing : boolean
>thing !== null : boolean
>thing : boolean
>null : null

Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(
a1(...array2); // Error parameter type is (number|string)[]
~~~~~~
!!! error TS2552: Cannot find name 'array2'. Did you mean 'Array'?
!!! related TS2728 /.ts/lib.es5.d.ts:1368:13: 'Array' is declared here.
!!! related TS2728 /.ts/lib.es5.d.ts:1374:13: 'Array' is declared here.
a5([1, 2, "string", false, true]); // Error, parameter type is [any, any, [[any]]]
~~~~~~~~
!!! error TS2322: Type 'string' is not assignable to type '[[any]]'.
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/promisePermutations.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ tests/cases/compiler/promisePermutations.ts(160,21): error TS2345: Argument of t
~~~~~~~~~
!!! error TS2345: Argument of type '(x: any) => IPromise<string>' is not assignable to parameter of type '(error: any) => Promise<number>'.
!!! error TS2345: Property 'catch' is missing in type 'IPromise<string>' but required in type 'Promise<number>'.
!!! related TS2728 /.ts/lib.es5.d.ts:1413:5: 'catch' is declared here.
!!! related TS2728 /.ts/lib.es5.d.ts:1419:5: 'catch' is declared here.
var s10g = s10.then(testFunctionP, nIPromise, sIPromise).then(sPromise, sIPromise, sIPromise); // ok

var r11: IPromise<number>;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/promisePermutations2.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ tests/cases/compiler/promisePermutations2.ts(159,21): error TS2345: Argument of
~~~~~~~~~
!!! error TS2345: Argument of type '(x: any) => IPromise<string>' is not assignable to parameter of type '(error: any) => Promise<number>'.
!!! error TS2345: Property 'catch' is missing in type 'IPromise<string>' but required in type 'Promise<number>'.
!!! related TS2728 /.ts/lib.es5.d.ts:1413:5: 'catch' is declared here.
!!! related TS2728 /.ts/lib.es5.d.ts:1419:5: 'catch' is declared here.
var s10g = s10.then(testFunctionP, nIPromise, sIPromise).then(sPromise, sIPromise, sIPromise); // ok

var r11: IPromise<number>;
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/promisePermutations3.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ tests/cases/compiler/promisePermutations3.ts(165,21): error TS2345: Argument of
~~~~~~~~~
!!! error TS2345: Argument of type '(x: any) => IPromise<string>' is not assignable to parameter of type '(error: any) => Promise<number>'.
!!! error TS2345: Property 'catch' is missing in type 'IPromise<string>' but required in type 'Promise<number>'.
!!! related TS2728 /.ts/lib.es5.d.ts:1413:5: 'catch' is declared here.
!!! related TS2728 /.ts/lib.es5.d.ts:1419:5: 'catch' is declared here.
var s10g = s10.then(testFunctionP, nIPromise, sIPromise).then(sPromise, sIPromise, sIPromise); // ok

var r11: IPromise<number>;
Expand Down Expand Up @@ -340,5 +340,5 @@ tests/cases/compiler/promisePermutations3.ts(165,21): error TS2345: Argument of
~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '{ <T>(x: T): IPromise<T>; <T>(x: T, y: T): Promise<T>; }' is not assignable to parameter of type '(value: (x: any) => any) => Promise<unknown>'.
!!! error TS2345: Property 'catch' is missing in type 'IPromise<any>' but required in type 'Promise<unknown>'.
!!! related TS2728 /.ts/lib.es5.d.ts:1413:5: 'catch' is declared here.
!!! related TS2728 /.ts/lib.es5.d.ts:1419:5: 'catch' is declared here.
var s12c = s12.then(testFunction12P, testFunction12, testFunction12); // ok
2 changes: 1 addition & 1 deletion tests/baselines/reference/promiseTypeInference.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ tests/cases/compiler/promiseTypeInference.ts(10,39): error TS2322: Type 'IPromis
!!! error TS2322: Types of parameters 'success' and 'onfulfilled' are incompatible.
!!! error TS2322: Type 'TResult1 | PromiseLike<TResult1>' is not assignable to type 'IPromise<TResult1 | TResult2>'.
!!! error TS2322: Type 'TResult1' is not assignable to type 'IPromise<TResult1 | TResult2>'.
!!! related TS6502 /.ts/lib.es5.d.ts:1406:57: The expected type comes from the return type of this signature.
!!! related TS6502 /.ts/lib.es5.d.ts:1412:57: The expected type comes from the return type of this signature.

2 changes: 1 addition & 1 deletion tests/baselines/reference/redefineArray.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ tests/cases/compiler/redefineArray.ts(1,1): error TS2741: Property 'isArray' is
Array = function (n:number, s:string) {return n;};
~~~~~
!!! error TS2741: Property 'isArray' is missing in type '(n: number, s: string) => number' but required in type 'ArrayConstructor'.
!!! related TS2728 /.ts/lib.es5.d.ts:1364:5: 'isArray' is declared here.
!!! related TS2728 /.ts/lib.es5.d.ts:1370:5: 'isArray' is declared here.
24 changes: 24 additions & 0 deletions tests/cases/compiler/booleanFilterAnyArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
interface Bullean { }
interface BulleanConstructor {
new(v1?: any): Bullean;
<T>(v2?: T): v2 is T;
}

interface Ari<T> {
filter<S extends T>(cb1: (value: T) => value is S): T extends any ? Ari<any> : Ari<S>;
filter(cb2: (value: T) => unknown): Ari<T>;
}
declare var Bullean: BulleanConstructor;
declare let anys: Ari<any>;
var xs: Ari<any>;
var xs = anys.filter(Bullean)

declare let realanys: any[];
var ys: any[];
var ys = realanys.filter(Boolean)

var foo = [{ name: 'x' }]
var foor: Array<{name: string}>
var foor = foo.filter(x => x.name)
var foos: Array<boolean>
var foos = [true, true, false, null].filter((thing): thing is boolean => thing !== null)