Skip to content

Commit 0a13549

Browse files
committed
PR-50377-redo-filterBooleanOverload
1 parent 3e12250 commit 0a13549

File tree

60 files changed

+1233
-530
lines changed

Some content is hidden

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

60 files changed

+1233
-530
lines changed

src/compiler/checker.ts

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14748,21 +14748,71 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1474814748
* Return the signatures of the given kind in the given type. Creates synthetic union signatures when necessary and
1474914749
* maps primitive types and type parameters are to their apparent types.
1475014750
*/
14751-
function getSignaturesOfType(type: Type, kind: SignatureKind): readonly Signature[] {
14752-
const result = getSignaturesOfStructuredType(getReducedApparentType(type), kind);
14753-
if (kind === SignatureKind.Call && !length(result) && type.flags & TypeFlags.Union) {
14754-
if ((type as UnionType).arrayFallbackSignatures) {
14755-
return (type as UnionType).arrayFallbackSignatures!;
14756-
}
14757-
// If the union is all different instantiations of a member of the global array type...
14758-
let memberName: __String;
14759-
if (everyType(type, t => !!t.symbol?.parent && isArrayOrTupleSymbol(t.symbol.parent) && (!memberName ? (memberName = t.symbol.escapedName, true) : memberName === t.symbol.escapedName))) {
14760-
// Transform the type from `(A[] | B[])["member"]` to `(A | B)[]["member"]` (since we pretend array is covariant anyway)
14761-
const arrayArg = mapType(type, t => getMappedType((isReadonlyArraySymbol(t.symbol.parent) ? globalReadonlyArrayType : globalArrayType).typeParameters![0], (t as AnonymousType).mapper!));
14762-
const arrayType = createArrayType(arrayArg, someType(type, t => isReadonlyArraySymbol(t.symbol.parent)));
14763-
return (type as UnionType).arrayFallbackSignatures = getSignaturesOfType(getTypeOfPropertyOfType(arrayType, memberName!)!, kind);
14764-
}
14765-
(type as UnionType).arrayFallbackSignatures = result;
14751+
function getSignaturesOfType(typeIn: Type, kind: SignatureKind): readonly Signature[] {
14752+
const reducedType = getReducedApparentType(typeIn);
14753+
function carveoutResult(): readonly Signature[] | undefined {
14754+
if (kind === SignatureKind.Call && reducedType.flags & TypeFlags.Union) {
14755+
// If the union is all different instantiations of a member of the global array type...
14756+
let memberName: __String;
14757+
if (everyType(reducedType, t => !!t.symbol?.parent && isArrayOrTupleSymbol(t.symbol.parent) && (!memberName ? (memberName = t.symbol.escapedName, true) : memberName === t.symbol.escapedName))) {
14758+
if ((reducedType as UnionType).arrayFallbackSignatures) {
14759+
return (reducedType as UnionType).arrayFallbackSignatures!;
14760+
}
14761+
//const numberOfOverloads = (reducedType as UnionType).types[0].symbol.declarations?.length;
14762+
14763+
// calculate return types as union of return types over the associated type, rather than return type associated with the union of types
14764+
const numTypes = (reducedType as UnionType).types.length;
14765+
const numSigs = (reducedType as UnionType).types[0].symbol.declarations?.length;
14766+
Debug.assert(numSigs);
14767+
const returnTypes = (new Array(numSigs).fill(/*value*/ undefined)).map((_,isig): Type | undefined =>{
14768+
// getUnionType(*,UnionReduction.Subtype) doesn't detect duplicate types (unless I'm mistaken) so use set first.
14769+
const rtset = new Set<Type>();
14770+
new Array(numTypes).fill(/*value*/ undefined).forEach((_,itype)=>{
14771+
// Notice we potentially generate the whole signature to get the return type.
14772+
// Maybe that could be avoided?
14773+
const callSignatures = ((reducedType as UnionType).types[itype] as ObjectType).callSignatures
14774+
?? getSignaturesOfStructuredType((reducedType as UnionType).types[itype] as ObjectType, SignatureKind.Call);
14775+
if (callSignatures[isig].typeParameters) return; // skip generic signatures, i.e. <S>
14776+
const returnType = callSignatures[isig].resolvedReturnType
14777+
?? getReturnTypeOfSignature(callSignatures[isig]);
14778+
Debug.assert(returnType);
14779+
rtset.add(returnType);
14780+
});
14781+
const art: Type[]=[];
14782+
rtset.forEach((rt)=>art.push(rt));
14783+
if (art.length===0) return undefined;
14784+
if (art.length===1) return art[0];
14785+
return getUnionType(art,UnionReduction.Subtype);
14786+
});
14787+
14788+
// Transform the type from `(A[] | B[])["member"]` to `(A | B)[]["member"]` (since we pretend array is covariant anyway)
14789+
const arrayArg = mapType(reducedType, t => getMappedType((isReadonlyArraySymbol(t.symbol.parent) ? globalReadonlyArrayType : globalArrayType).typeParameters![0], (t as AnonymousType).mapper!));
14790+
const arrayType = createArrayType(arrayArg, someType(reducedType, t => isReadonlyArraySymbol(t.symbol.parent)));
14791+
const result = getSignaturesOfType(getTypeOfPropertyOfType(arrayType, memberName!)!, kind);
14792+
14793+
returnTypes.forEach((returnType,sigidx) => {
14794+
if (returnType) result[sigidx].resolvedReturnType = returnTypes[sigidx];
14795+
});
14796+
return (reducedType as UnionType).arrayFallbackSignatures = result;
14797+
14798+
// const arrayArg = mapType(type, t => getMappedType((isReadonlyArraySymbol(t.symbol.parent) ? globalReadonlyArrayType : globalArrayType).typeParameters![0], (t as AnonymousType).mapper!));
14799+
// const arrayType = createArrayType(arrayArg, someType(type, t => isReadonlyArraySymbol(t.symbol.parent)));
14800+
// return (type as UnionType).arrayFallbackSignatures = getSignaturesOfType(getTypeOfPropertyOfType(arrayType, memberName!)!, kind);
14801+
}
14802+
}
14803+
return undefined;
14804+
}
14805+
let result = carveoutResult();
14806+
if (result) return result;
14807+
14808+
if (kind === SignatureKind.Call && reducedType.flags & TypeFlags.Union) {
14809+
if ((reducedType as UnionType).arrayFallbackSignatures) {
14810+
return (reducedType as UnionType).arrayFallbackSignatures!;
14811+
}
14812+
}
14813+
result = getSignaturesOfStructuredType(reducedType, kind);
14814+
if (kind === SignatureKind.Call && reducedType.flags & TypeFlags.Union) {
14815+
(reducedType as UnionType).arrayFallbackSignatures = result;
1476614816
}
1476714817
return result;
1476814818
}

src/lib/es5.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,12 @@ interface ReadonlyArray<T> {
12571257
* @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.
12581258
*/
12591259
filter<S extends T>(predicate: (value: T, index: number, array: readonly T[]) => value is S, thisArg?: any): S[];
1260+
/**
1261+
* Returns the non-Falsy elements of an array
1262+
* @param predicate Must be exactly "Boolean"
1263+
* @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.
1264+
*/
1265+
filter(predicate: BooleanConstructor, thisArg?: any): (T extends false | 0 | "" | null | undefined | 0n ? never : T)[];
12601266
/**
12611267
* Returns the elements of an array that meet the condition specified in a callback function.
12621268
* @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.
@@ -1448,6 +1454,12 @@ interface Array<T> {
14481454
* @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.
14491455
*/
14501456
filter<S extends T>(predicate: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[];
1457+
/**
1458+
* Returns the non-Falsy elements of an array
1459+
* @param predicate Must be exactly "Boolean"
1460+
* @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.
1461+
*/
1462+
filter(predicate: BooleanConstructor, thisArg?: any): (T extends false | 0 | "" | null | undefined | 0n ? never : T)[];
14511463
/**
14521464
* Returns the elements of an array that meet the condition specified in a callback function.
14531465
* @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.

tests/baselines/reference/arrayFilter.symbols

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ var foo = [
1616
]
1717

1818
foo.filter(x => x.name); //should accepted all possible types not only boolean!
19-
>foo.filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
19+
>foo.filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
2020
>foo : Symbol(foo, Decl(arrayFilter.ts, 0, 3))
21-
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
21+
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
2222
>x : Symbol(x, Decl(arrayFilter.ts, 6, 11))
2323
>x.name : Symbol(name, Decl(arrayFilter.ts, 1, 5))
2424
>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
@@ -23,9 +23,9 @@ var foo = [
2323

2424
foo.filter(x => x.name); //should accepted all possible types not only boolean!
2525
>foo.filter(x => x.name) : { name: string; }[]
26-
>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; }[]; }
26+
>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; }[]; }
2727
>foo : { name: string; }[]
28-
>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; }[]; }
28+
>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; }[]; }
2929
>x => x.name : (x: { name: string; }) => string
3030
>x : { name: string; }
3131
>x.name : string
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//// [tests/cases/compiler/arrayFilterBooleanOverload#56013WithoutExternalOverload.ts] ////
2+
3+
//// [arrayFilterBooleanOverload#56013WithoutExternalOverload.ts]
4+
type NonFalsy<T> = T extends false | 0 | "" | null | undefined | 0n
5+
? never
6+
: T;
7+
8+
const id = <T,>() => (t: T) => !!t;
9+
10+
['foo', 'bar'].filter(id()); // // expect id() = (t: string) => boolean
11+
12+
['foo', 'bar', 1].filter(id()); // // expect id() = (t: string | number) => boolean
13+
14+
declare const maybe: boolean;
15+
(maybe ? ['foo', 'bar'] : [1] ).filter(id()); // expect id() = (t: string | number) => boolean
16+
17+
['foo', 'bar', undefined].filter(id()); // expect id() = (t: string | undefined) => boolean
18+
19+
20+
//// [arrayFilterBooleanOverload#56013WithoutExternalOverload.js]
21+
"use strict";
22+
const id = () => (t) => !!t;
23+
['foo', 'bar'].filter(id()); // // expect id() = (t: string) => boolean
24+
['foo', 'bar', 1].filter(id()); // // expect id() = (t: string | number) => boolean
25+
(maybe ? ['foo', 'bar'] : [1]).filter(id()); // expect id() = (t: string | number) => boolean
26+
['foo', 'bar', undefined].filter(id()); // expect id() = (t: string | undefined) => boolean
27+
28+
29+
//// [arrayFilterBooleanOverload#56013WithoutExternalOverload.d.ts]
30+
type NonFalsy<T> = T extends false | 0 | "" | null | undefined | 0n ? never : T;
31+
declare const id: <T>() => (t: T) => boolean;
32+
declare const maybe: boolean;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//// [tests/cases/compiler/arrayFilterBooleanOverload#56013WithoutExternalOverload.ts] ////
2+
3+
=== arrayFilterBooleanOverload#56013WithoutExternalOverload.ts ===
4+
type NonFalsy<T> = T extends false | 0 | "" | null | undefined | 0n
5+
>NonFalsy : Symbol(NonFalsy, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 0, 0))
6+
>T : Symbol(T, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 0, 14))
7+
>T : Symbol(T, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 0, 14))
8+
9+
? never
10+
: T;
11+
>T : Symbol(T, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 0, 14))
12+
13+
const id = <T,>() => (t: T) => !!t;
14+
>id : Symbol(id, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 4, 5))
15+
>T : Symbol(T, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 4, 12))
16+
>t : Symbol(t, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 4, 22))
17+
>T : Symbol(T, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 4, 12))
18+
>t : Symbol(t, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 4, 22))
19+
20+
['foo', 'bar'].filter(id()); // // expect id() = (t: string) => boolean
21+
>['foo', 'bar'].filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
22+
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
23+
>id : Symbol(id, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 4, 5))
24+
25+
['foo', 'bar', 1].filter(id()); // // expect id() = (t: string | number) => boolean
26+
>['foo', 'bar', 1].filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
27+
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
28+
>id : Symbol(id, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 4, 5))
29+
30+
declare const maybe: boolean;
31+
>maybe : Symbol(maybe, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 10, 13))
32+
33+
(maybe ? ['foo', 'bar'] : [1] ).filter(id()); // expect id() = (t: string | number) => boolean
34+
>(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)
35+
>maybe : Symbol(maybe, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 10, 13))
36+
>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)
37+
>id : Symbol(id, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 4, 5))
38+
39+
['foo', 'bar', undefined].filter(id()); // expect id() = (t: string | undefined) => boolean
40+
>['foo', 'bar', undefined].filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
41+
>undefined : Symbol(undefined)
42+
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
43+
>id : Symbol(id, Decl(arrayFilterBooleanOverload#56013WithoutExternalOverload.ts, 4, 5))
44+

0 commit comments

Comments
 (0)