Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The !!getIndexTypeOfType(t, numberType) bit here is required to cover this concatTuples test case. .concat is typed using ConcatArray which isn't exactly an array.

const hasIterable = getGlobalIterableType(/* reportErrors */ false) !== emptyGenericType;
Comment thread
jakebailey marked this conversation as resolved.
Outdated
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
},
]);