Skip to content

Commit

Permalink
Filter out non-array types when contextually typing union members
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Feb 3, 2023
1 parent bbfb9ac commit 8ebc95f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28838,8 +28838,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// it is the type of the numeric index signature in T. Otherwise, in ES6 and higher, the contextual type is the iterated
// type of T.
function getContextualTypeForElementExpression(arrayContextualType: Type | undefined, index: number): Type | undefined {
const hasIterable = getGlobalIterableType(/* reportErrors */ false) !== emptyGenericType;
return arrayContextualType && (
getTypeOfPropertyOfContextualType(arrayContextualType, "" + index as __String)
getTypeOfPropertyOfContextualType(filterType(arrayContextualType, t => isArrayOrTupleLikeType(t) || !!getIndexTypeOfType(t, numberType) || hasIterable && isTypeAssignableTo(t, createIterableType(anyType))), "" + index as __String)
|| mapType(
arrayContextualType,
t => getIteratedTypeOrElementType(IterationUse.Element, t, undefinedType, /*errorNode*/ undefined, /*checkAssignability*/ false),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/compiler/contextualSignatureInArrayElementPrefersArrayUnionMember.ts ===
// repro from #52588

declare function test(
>test : Symbol(test, Decl(contextualSignatureInArrayElementPrefersArrayUnionMember.ts, 0, 0))

arg: Record<string, (arg: string) => void> | Array<(arg: number) => void>
>arg : Symbol(arg, Decl(contextualSignatureInArrayElementPrefersArrayUnionMember.ts, 2, 22))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
>arg : Symbol(arg, Decl(contextualSignatureInArrayElementPrefersArrayUnionMember.ts, 3, 23))
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>arg : Symbol(arg, Decl(contextualSignatureInArrayElementPrefersArrayUnionMember.ts, 3, 54))

): void;

test([
>test : Symbol(test, Decl(contextualSignatureInArrayElementPrefersArrayUnionMember.ts, 0, 0))

(arg) => {
>arg : Symbol(arg, Decl(contextualSignatureInArrayElementPrefersArrayUnionMember.ts, 7, 3))

arg; // number
>arg : Symbol(arg, Decl(contextualSignatureInArrayElementPrefersArrayUnionMember.ts, 7, 3))

},
]);

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
=== tests/cases/compiler/contextualSignatureInArrayElementPrefersArrayUnionMember.ts ===
// repro from #52588

declare function test(
>test : (arg: Record<string, (arg: string) => void> | ((arg: number) => void)[]) => void

arg: Record<string, (arg: string) => void> | Array<(arg: number) => void>
>arg : Record<string, (arg: string) => void> | ((arg: number) => void)[]
>arg : string
>arg : number

): void;

test([
>test([ (arg) => { arg; // number },]) : void
>test : (arg: Record<string, (arg: string) => void> | ((arg: number) => void)[]) => void
>[ (arg) => { arg; // number },] : ((arg: number) => void)[]

(arg) => {
>(arg) => { arg; // number } : (arg: number) => void
>arg : number

arg; // number
>arg : number

},
]);

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @strict: true
// @noEmit: true

// repro from #52588

declare function test(
arg: Record<string, (arg: string) => void> | Array<(arg: number) => void>
): void;

test([
(arg) => {
arg; // number
},
]);

0 comments on commit 8ebc95f

Please sign in to comment.