Skip to content

Commit

Permalink
new es5.d.ts, checker.ts, and correponding test results
Browse files Browse the repository at this point in the history
  • Loading branch information
craigphicks committed Nov 4, 2023
1 parent 4130dea commit 64d8ace
Show file tree
Hide file tree
Showing 55 changed files with 778 additions and 622 deletions.
79 changes: 64 additions & 15 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14748,21 +14748,70 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
* Return the signatures of the given kind in the given type. Creates synthetic union signatures when necessary and
* 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) {
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);
}
(type as UnionType).arrayFallbackSignatures = result;
function getSignaturesOfType(typeIn: Type, kind: SignatureKind): readonly Signature[] {
const reducedType = getReducedApparentType(typeIn);
function carveoutResult(): readonly Signature[] | undefined {
if (kind === SignatureKind.Call && reducedType.flags & TypeFlags.Union) {
// If the union is all different instantiations of a member of the global array type...
let memberName: __String;
if (everyType(reducedType, t => !!t.symbol?.parent && isArrayOrTupleSymbol(t.symbol.parent) && (!memberName ? (memberName = t.symbol.escapedName, true) : memberName === t.symbol.escapedName))) {
if ((reducedType as UnionType).arrayFallbackSignatures) {
return (reducedType as UnionType).arrayFallbackSignatures!;
}

// calculate return types as union of return types over the associated type, rather than return type associated with the union of types
const numTypes = (reducedType as UnionType).types.length;
const numSigs = (reducedType as UnionType).types[0].symbol.declarations?.length;
Debug.assert(numSigs);
const returnTypes = (new Array(numSigs).fill(/*value*/ undefined)).map((_, isig): Type | undefined => {
// getUnionType(*,UnionReduction.Subtype) doesn't detect duplicate types (unless I'm mistaken) so use set first.
const rtset = new Set<Type>();
new Array(numTypes).fill(/*value*/ undefined).forEach((_, itype) => {
// Notice we potentially generate the whole signature to get the return type.
// Maybe that could be avoided?
const callSignatures = ((reducedType as UnionType).types[itype] as ObjectType).callSignatures
?? getSignaturesOfStructuredType((reducedType as UnionType).types[itype] as ObjectType, SignatureKind.Call);
if (callSignatures[isig].typeParameters) return; // skip generic signatures, i.e. <S>
const returnType = callSignatures[isig].resolvedReturnType
?? getReturnTypeOfSignature(callSignatures[isig]);
Debug.assert(returnType);
rtset.add(returnType);
});
const art: Type[] = [];
rtset.forEach(rt => art.push(rt));
if (art.length === 0) return undefined;
if (art.length === 1) return art[0];
return getUnionType(art, UnionReduction.Subtype);
});

// Transform the type from `(A[] | B[])["member"]` to `(A | B)[]["member"]` (since we pretend array is covariant anyway)
const arrayArg = mapType(reducedType, t => getMappedType((isReadonlyArraySymbol(t.symbol.parent) ? globalReadonlyArrayType : globalArrayType).typeParameters![0], (t as AnonymousType).mapper!));
const arrayType = createArrayType(arrayArg, someType(reducedType, t => isReadonlyArraySymbol(t.symbol.parent)));
const result = getSignaturesOfType(getTypeOfPropertyOfType(arrayType, memberName!)!, kind);

returnTypes.forEach((returnType, sigidx) => {
if (returnType) result[sigidx].resolvedReturnType = returnTypes[sigidx];
});
return (reducedType as UnionType).arrayFallbackSignatures = result;

// 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();
if (result) return result;

if (kind === SignatureKind.Call && reducedType.flags & TypeFlags.Union) {
if ((reducedType as UnionType).arrayFallbackSignatures) {
return (reducedType as UnionType).arrayFallbackSignatures!;
}
}
result = getSignaturesOfStructuredType(reducedType, kind);
if (kind === SignatureKind.Call && reducedType.flags & TypeFlags.Union) {
(reducedType as UnionType).arrayFallbackSignatures = result;
}
return result;
}
Expand Down
12 changes: 12 additions & 0 deletions src/lib/es5.d.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ const id = <T,>() => (t: T) => !!t;
>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, --, --))
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>['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, --, --))
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>['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, --, --))
>(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, --, --))
>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, --, --))
>['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, --, --))
>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))

Loading

0 comments on commit 64d8ace

Please sign in to comment.