Skip to content

Commit

Permalink
PR-50377-redo-filterBooleanOverload
Browse files Browse the repository at this point in the history
  • Loading branch information
craigphicks committed Nov 2, 2023
1 parent 3e12250 commit 28da703
Show file tree
Hide file tree
Showing 61 changed files with 1,211 additions and 539 deletions.
39 changes: 29 additions & 10 deletions src/compiler/checker.ts
Expand Up @@ -14749,19 +14749,38 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
* maps primitive types and type parameters are to their apparent types.
*/
function getSignaturesOfType(type: Type, kind: SignatureKind): readonly Signature[] {
const result = getSignaturesOfStructuredType(getReducedApparentType(type), kind);
if (kind === SignatureKind.Call && !length(result) && type.flags & TypeFlags.Union) {
function carveoutResult(type: Type, kind: SignatureKind): readonly Signature[] | undefined {
if (kind === SignatureKind.Call && type.flags & TypeFlags.Union) {
// If the union is all different instantiations of a member of the global array type...
let memberName: __String;
if (everyType(type, t => !!t.symbol?.parent && isArrayOrTupleSymbol(t.symbol.parent) && (!memberName ? (memberName = t.symbol.escapedName, true) : memberName === t.symbol.escapedName))) {
if ((type as UnionType).arrayFallbackSignatures) {
return (type as UnionType).arrayFallbackSignatures!;
}
const arrayArg = mapType(type, t => getMappedType((isReadonlyArraySymbol(t.symbol.parent) ? globalReadonlyArrayType : globalArrayType).typeParameters![0], (t as AnonymousType).mapper!));
const arrayType = createArrayType(arrayArg, someType(type, t => isReadonlyArraySymbol(t.symbol.parent)));
return (type as UnionType).arrayFallbackSignatures = getSignaturesOfType(getTypeOfPropertyOfType(arrayType, memberName!)!, kind);
}
}
return undefined;
}
let result = carveoutResult(type, kind);
if (result) return result;
// The original logical for the non-carveout case is recorded in the block comment below, for the record.
// result = getSignaturesOfStructuredType(getReducedApparentType(type), kind);
// if (kind === SignatureKind.Call && !length(result) && type.flags & TypeFlags.Union) {
// if ((type as UnionType).arrayFallbackSignatures) {
// return (type as UnionType).arrayFallbackSignatures!;
// }
// return (type as UnionType).arrayFallbackSignatures = result;
// }
if (kind === SignatureKind.Call && type.flags & TypeFlags.Union) {
if ((type as UnionType).arrayFallbackSignatures) {
return (type as UnionType).arrayFallbackSignatures!;
}
// If the union is all different instantiations of a member of the global array type...
let memberName: __String;
if (everyType(type, t => !!t.symbol?.parent && isArrayOrTupleSymbol(t.symbol.parent) && (!memberName ? (memberName = t.symbol.escapedName, true) : memberName === t.symbol.escapedName))) {
// Transform the type from `(A[] | B[])["member"]` to `(A | B)[]["member"]` (since we pretend array is covariant anyway)
const arrayArg = mapType(type, t => getMappedType((isReadonlyArraySymbol(t.symbol.parent) ? globalReadonlyArrayType : globalArrayType).typeParameters![0], (t as AnonymousType).mapper!));
const arrayType = createArrayType(arrayArg, someType(type, t => isReadonlyArraySymbol(t.symbol.parent)));
return (type as UnionType).arrayFallbackSignatures = getSignaturesOfType(getTypeOfPropertyOfType(arrayType, memberName!)!, kind);
}
}
result = getSignaturesOfStructuredType(getReducedApparentType(type), kind);
if (kind === SignatureKind.Call && type.flags & TypeFlags.Union) {
(type as UnionType).arrayFallbackSignatures = result;
}
return result;
Expand Down
12 changes: 12 additions & 0 deletions src/lib/es5.d.ts
Expand Up @@ -1257,6 +1257,12 @@ interface ReadonlyArray<T> {
* @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.
*/
filter<S extends T>(predicate: (value: T, index: number, array: readonly T[]) => value is S, thisArg?: any): S[];
/**
* Returns the non-Falsy elements of an array
* @param predicate Must be exactly "Boolean"
* @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.
*/
filter(predicate: BooleanConstructor, thisArg?: any): (T extends false | 0 | "" | null | undefined | 0n ? never : T)[];
/**
* Returns the elements of an array that meet the condition specified in a callback function.
* @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.
Expand Down Expand Up @@ -1448,6 +1454,12 @@ interface Array<T> {
* @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.
*/
filter<S extends T>(predicate: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[];
/**
* Returns the non-Falsy elements of an array
* @param predicate Must be exactly "Boolean"
* @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.
*/
filter(predicate: BooleanConstructor, thisArg?: any): (T extends false | 0 | "" | null | undefined | 0n ? never : T)[];
/**
* Returns the elements of an array that meet the condition specified in a callback function.
* @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.
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/arrayFilter.symbols
Expand Up @@ -16,9 +16,9 @@ var foo = [
]

foo.filter(x => x.name); //should accepted all possible types not only boolean!
>foo.filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>foo.filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>foo : Symbol(foo, Decl(arrayFilter.ts, 0, 3))
>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, --, --), Decl(lib.es5.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
Expand Up @@ -23,9 +23,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 : { <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; }[]; }
>foo.filter : { <S extends { name: string; }>(predicate: (value: { name: string; }, index: number, array: { name: string; }[]) => value is S, thisArg?: any): S[]; (predicate: BooleanConstructor, thisArg?: any): { name: string; }[]; (predicate: (value: { name: string; }, index: number, array: { name: string; }[]) => unknown, thisArg?: any): { name: string; }[]; }
>foo : { name: string; }[]
>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; }[]; }
>filter : { <S extends { name: string; }>(predicate: (value: { name: string; }, index: number, array: { name: string; }[]) => value is S, thisArg?: any): S[]; (predicate: BooleanConstructor, thisArg?: any): { name: string; }[]; (predicate: (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
Expand Down
@@ -0,0 +1,32 @@
//// [tests/cases/compiler/arrayFilterBooleanOverload#56013WithoutExternalOverload.ts] ////

//// [arrayFilterBooleanOverload#56013WithoutExternalOverload.ts]
type NonFalsy<T> = T extends false | 0 | "" | null | undefined | 0n
? never
: T;

const id = <T,>() => (t: T) => !!t;

['foo', 'bar'].filter(id()); // // expect id() = (t: string) => boolean

['foo', 'bar', 1].filter(id()); // // expect id() = (t: string | number) => boolean

declare const maybe: boolean;
(maybe ? ['foo', 'bar'] : [1] ).filter(id()); // expect id() = (t: string | number) => boolean

['foo', 'bar', undefined].filter(id()); // expect id() = (t: string | undefined) => boolean


//// [arrayFilterBooleanOverload#56013WithoutExternalOverload.js]
"use strict";
const id = () => (t) => !!t;
['foo', 'bar'].filter(id()); // // expect id() = (t: string) => boolean
['foo', 'bar', 1].filter(id()); // // expect id() = (t: string | number) => boolean
(maybe ? ['foo', 'bar'] : [1]).filter(id()); // expect id() = (t: string | number) => boolean
['foo', 'bar', undefined].filter(id()); // expect id() = (t: string | undefined) => boolean


//// [arrayFilterBooleanOverload#56013WithoutExternalOverload.d.ts]
type NonFalsy<T> = T extends false | 0 | "" | null | undefined | 0n ? never : T;
declare const id: <T>() => (t: T) => boolean;
declare const maybe: boolean;
@@ -0,0 +1,44 @@
//// [tests/cases/compiler/arrayFilterBooleanOverload#56013WithoutExternalOverload.ts] ////

=== arrayFilterBooleanOverload#56013WithoutExternalOverload.ts ===
type NonFalsy<T> = T extends false | 0 | "" | null | undefined | 0n
>NonFalsy : Symbol(NonFalsy, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 0, 0))
>T : Symbol(T, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 0, 14))
>T : Symbol(T, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 0, 14))

? never
: T;
>T : Symbol(T, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 0, 14))

const id = <T,>() => (t: T) => !!t;
>id : Symbol(id, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 4, 5))
>T : Symbol(T, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 4, 12))
>t : Symbol(t, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 4, 22))
>T : Symbol(T, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 4, 12))
>t : Symbol(t, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 4, 22))

['foo', 'bar'].filter(id()); // // expect id() = (t: string) => boolean
>['foo', 'bar'].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, --, --))
>id : Symbol(id, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 4, 5))

['foo', 'bar', 1].filter(id()); // // expect id() = (t: string | number) => boolean
>['foo', 'bar', 1].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, --, --))
>id : Symbol(id, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 4, 5))

declare const maybe: boolean;
>maybe : Symbol(maybe, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 10, 13))

(maybe ? ['foo', 'bar'] : [1] ).filter(id()); // expect id() = (t: string | number) => boolean
>(maybe ? ['foo', 'bar'] : [1] ).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, --, --) ... and 1 more)
>maybe : Symbol(maybe, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 10, 13))
>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, --, --) ... and 1 more)
>id : Symbol(id, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 4, 5))

['foo', 'bar', undefined].filter(id()); // expect id() = (t: string | undefined) => boolean
>['foo', 'bar', undefined].filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>undefined : Symbol(undefined)
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>id : Symbol(id, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 4, 5))

@@ -0,0 +1,69 @@
//// [tests/cases/compiler/arrayFilterBooleanOverload#56013WithoutExternalOverload.ts] ////

=== arrayFilterBooleanOverload#56013WithoutExternalOverload.ts ===
type NonFalsy<T> = T extends false | 0 | "" | null | undefined | 0n
>NonFalsy : NonFalsy<T>
>false : false

? never
: T;

const id = <T,>() => (t: T) => !!t;
>id : <T>() => (t: T) => boolean
><T,>() => (t: T) => !!t : <T>() => (t: T) => boolean
>(t: T) => !!t : (t: T) => boolean
>t : T
>!!t : boolean
>!t : boolean
>t : T

['foo', 'bar'].filter(id()); // // expect id() = (t: string) => boolean
>['foo', 'bar'].filter(id()) : string[]
>['foo', 'bar'].filter : { <S extends string>(predicate: (value: string, index: number, array: string[]) => value is S, thisArg?: any): S[]; (predicate: BooleanConstructor, thisArg?: any): string[]; (predicate: (value: string, index: number, array: string[]) => unknown, thisArg?: any): string[]; }
>['foo', 'bar'] : string[]
>'foo' : "foo"
>'bar' : "bar"
>filter : { <S extends string>(predicate: (value: string, index: number, array: string[]) => value is S, thisArg?: any): S[]; (predicate: BooleanConstructor, thisArg?: any): string[]; (predicate: (value: string, index: number, array: string[]) => unknown, thisArg?: any): string[]; }
>id() : (t: string) => boolean
>id : <T>() => (t: T) => boolean

['foo', 'bar', 1].filter(id()); // // expect id() = (t: string | number) => boolean
>['foo', 'bar', 1].filter(id()) : (string | number)[]
>['foo', 'bar', 1].filter : { <S extends string | number>(predicate: (value: string | number, index: number, array: (string | number)[]) => value is S, thisArg?: any): S[]; (predicate: BooleanConstructor, thisArg?: any): (string | number)[]; (predicate: (value: string | number, index: number, array: (string | number)[]) => unknown, thisArg?: any): (string | number)[]; }
>['foo', 'bar', 1] : (string | number)[]
>'foo' : "foo"
>'bar' : "bar"
>1 : 1
>filter : { <S extends string | number>(predicate: (value: string | number, index: number, array: (string | number)[]) => value is S, thisArg?: any): S[]; (predicate: BooleanConstructor, thisArg?: any): (string | number)[]; (predicate: (value: string | number, index: number, array: (string | number)[]) => unknown, thisArg?: any): (string | number)[]; }
>id() : (t: string | number) => boolean
>id : <T>() => (t: T) => boolean

declare const maybe: boolean;
>maybe : boolean

(maybe ? ['foo', 'bar'] : [1] ).filter(id()); // expect id() = (t: string | number) => boolean
>(maybe ? ['foo', 'bar'] : [1] ).filter(id()) : (string | number)[]
>(maybe ? ['foo', 'bar'] : [1] ).filter : { <S extends string>(predicate: (value: string, index: number, array: string[]) => value is S, thisArg?: any): S[]; (predicate: BooleanConstructor, thisArg?: any): string[]; (predicate: (value: string, index: number, array: string[]) => unknown, thisArg?: any): string[]; } | { <S extends number>(predicate: (value: number, index: number, array: number[]) => value is S, thisArg?: any): S[]; (predicate: BooleanConstructor, thisArg?: any): number[]; (predicate: (value: number, index: number, array: number[]) => unknown, thisArg?: any): number[]; }
>(maybe ? ['foo', 'bar'] : [1] ) : string[] | number[]
>maybe ? ['foo', 'bar'] : [1] : string[] | number[]
>maybe : boolean
>['foo', 'bar'] : string[]
>'foo' : "foo"
>'bar' : "bar"
>[1] : number[]
>1 : 1
>filter : { <S extends string>(predicate: (value: string, index: number, array: string[]) => value is S, thisArg?: any): S[]; (predicate: BooleanConstructor, thisArg?: any): string[]; (predicate: (value: string, index: number, array: string[]) => unknown, thisArg?: any): string[]; } | { <S extends number>(predicate: (value: number, index: number, array: number[]) => value is S, thisArg?: any): S[]; (predicate: BooleanConstructor, thisArg?: any): number[]; (predicate: (value: number, index: number, array: number[]) => unknown, thisArg?: any): number[]; }
>id() : (t: string | number) => boolean
>id : <T>() => (t: T) => boolean

['foo', 'bar', undefined].filter(id()); // expect id() = (t: string | undefined) => boolean
>['foo', 'bar', undefined].filter(id()) : (string | undefined)[]
>['foo', 'bar', undefined].filter : { <S extends string | undefined>(predicate: (value: string | undefined, index: number, array: (string | undefined)[]) => value is S, thisArg?: any): S[]; (predicate: BooleanConstructor, thisArg?: any): string[]; (predicate: (value: string | undefined, index: number, array: (string | undefined)[]) => unknown, thisArg?: any): (string | undefined)[]; }
>['foo', 'bar', undefined] : (string | undefined)[]
>'foo' : "foo"
>'bar' : "bar"
>undefined : undefined
>filter : { <S extends string | undefined>(predicate: (value: string | undefined, index: number, array: (string | undefined)[]) => value is S, thisArg?: any): S[]; (predicate: BooleanConstructor, thisArg?: any): string[]; (predicate: (value: string | undefined, index: number, array: (string | undefined)[]) => unknown, thisArg?: any): (string | undefined)[]; }
>id() : (t: string | undefined) => boolean
>id : <T>() => (t: T) => boolean

32 changes: 32 additions & 0 deletions tests/baselines/reference/arrayFilterBooleanOverload.js
@@ -0,0 +1,32 @@
//// [tests/cases/compiler/arrayFilterBooleanOverload.ts] ////

//// [arrayFilterBooleanOverload.ts]
const nullableValues = ['a', 'b', null]; // expect (string | null)[]

const values1 = nullableValues.filter(Boolean); // expect string[]

// @ts-expect-error
const values2 = nullableValues.filter(new Boolean);

const arr = [0, 1, "", "foo", null] as const;

const arr2 = arr.filter(Boolean); // expect ("foo" | 1)[]



//// [arrayFilterBooleanOverload.js]
"use strict";
const nullableValues = ['a', 'b', null]; // expect (string | null)[]
const values1 = nullableValues.filter(Boolean); // expect string[]
// @ts-expect-error
const values2 = nullableValues.filter(new Boolean);
const arr = [0, 1, "", "foo", null];
const arr2 = arr.filter(Boolean); // expect ("foo" | 1)[]


//// [arrayFilterBooleanOverload.d.ts]
declare const nullableValues: (string | null)[];
declare const values1: string[];
declare const values2: (string | null)[];
declare const arr: readonly [0, 1, "", "foo", null];
declare const arr2: (1 | "foo")[];

0 comments on commit 28da703

Please sign in to comment.