TypeScript/tests/cases/compiler/booleanFilterAnyArray.ts
Nathan Shively-Sanders b36c8a0690
Make anyArray.filter(Boolean) return any[], not unknown[] (#31515)
* Add this-parameter workaround to Array.filter

Allows anys.filter(Boolean) to once again return any[], not unknown[].

* Add any constraint to Boolean factory function

I want to test how well this works.

* Remove Boolean factory type guard

* Remove typeGuardBoolean test
2019-05-22 09:45:41 -07:00

25 lines
676 B
TypeScript

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)