Skip to content

Commit 54b5ad0

Browse files
committed
Prefer non-partial signature match in non-generic case
1 parent 3af710e commit 54b5ad0

File tree

5 files changed

+65
-14
lines changed

5 files changed

+65
-14
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13019,9 +13019,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1301913019
}
1302013020
let result: Signature[] | undefined;
1302113021
for (let i = 0; i < signatureLists.length; i++) {
13022-
// Allow matching non-generic signatures to have excess parameters and different return types.
13022+
// Allow matching non-generic signatures to have excess parameters (as a fallback if exact parameter match is not found) and different return types.
1302313023
// Prefer matching this types if possible.
13024-
const match = i === listIndex ? signature : findMatchingSignature(signatureLists[i], signature, /*partialMatch*/ true, /*ignoreThisTypes*/ false, /*ignoreReturnTypes*/ true);
13024+
const match = i === listIndex
13025+
? signature
13026+
: findMatchingSignature(signatureLists[i], signature, /*partialMatch*/ false, /*ignoreThisTypes*/ false, /*ignoreReturnTypes*/ true)
13027+
|| findMatchingSignature(signatureLists[i], signature, /*partialMatch*/ true, /*ignoreThisTypes*/ false, /*ignoreReturnTypes*/ true);
1302513028
if (!match) {
1302613029
return undefined;
1302713030
}

tests/baselines/reference/unionTypeCallSignatures6.errors.txt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@ unionTypeCallSignatures6.ts(38,4): error TS2349: This expression is not callable
66
unionTypeCallSignatures6.ts(39,1): error TS2684: The 'this' context of type 'A & C & { f0: F0 | F3; f1: F1 | F3; f2: F1 | F4; f3: F3 | F4; f4: F3 | F5; }' is not assignable to method's 'this' of type 'B'.
77
Property 'b' is missing in type 'A & C & { f0: F0 | F3; f1: F1 | F3; f2: F1 | F4; f3: F3 | F4; f4: F3 | F5; }' but required in type 'B'.
88
unionTypeCallSignatures6.ts(48,1): error TS2684: The 'this' context of type 'void' is not assignable to method's 'this' of type 'A & B'.
9-
unionTypeCallSignatures6.ts(55,1): error TS2769: No overload matches this call.
10-
Overload 1 of 2, '(this: A & B & C): void', gave the following error.
11-
The 'this' context of type 'void' is not assignable to method's 'this' of type 'A & B & C'.
12-
Type 'void' is not assignable to type 'A'.
13-
Overload 2 of 2, '(this: A & B): void', gave the following error.
14-
The 'this' context of type 'void' is not assignable to method's 'this' of type 'A & B'.
9+
unionTypeCallSignatures6.ts(55,1): error TS2684: The 'this' context of type 'void' is not assignable to method's 'this' of type 'A & B'.
1510

1611

1712
==== unionTypeCallSignatures6.ts (6 errors) ====
@@ -85,10 +80,5 @@ unionTypeCallSignatures6.ts(55,1): error TS2769: No overload matches this call.
8580
declare var f4: F6 | F7;
8681
f4(); // error
8782
~~~~
88-
!!! error TS2769: No overload matches this call.
89-
!!! error TS2769: Overload 1 of 2, '(this: A & B & C): void', gave the following error.
90-
!!! error TS2769: The 'this' context of type 'void' is not assignable to method's 'this' of type 'A & B & C'.
91-
!!! error TS2769: Type 'void' is not assignable to type 'A'.
92-
!!! error TS2769: Overload 2 of 2, '(this: A & B): void', gave the following error.
93-
!!! error TS2769: The 'this' context of type 'void' is not assignable to method's 'this' of type 'A & B'.
83+
!!! error TS2684: The 'this' context of type 'void' is not assignable to method's 'this' of type 'A & B'.
9484

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//// [tests/cases/conformance/types/union/unionTypeCallSignatures7.ts] ////
2+
3+
=== unionTypeCallSignatures7.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/55203
5+
6+
interface Callable<Name extends string> {
7+
>Callable : Symbol(Callable, Decl(unionTypeCallSignatures7.ts, 0, 0))
8+
>Name : Symbol(Name, Decl(unionTypeCallSignatures7.ts, 2, 19))
9+
10+
(): `${Name} without id`;
11+
>Name : Symbol(Name, Decl(unionTypeCallSignatures7.ts, 2, 19))
12+
13+
(id: number): `${Name} with id`;
14+
>id : Symbol(id, Decl(unionTypeCallSignatures7.ts, 4, 3))
15+
>Name : Symbol(Name, Decl(unionTypeCallSignatures7.ts, 2, 19))
16+
}
17+
18+
declare const f: Callable<"A"> | Callable<"B">;
19+
>f : Symbol(f, Decl(unionTypeCallSignatures7.ts, 7, 13))
20+
>Callable : Symbol(Callable, Decl(unionTypeCallSignatures7.ts, 0, 0))
21+
>Callable : Symbol(Callable, Decl(unionTypeCallSignatures7.ts, 0, 0))
22+
23+
const result = f(123);
24+
>result : Symbol(result, Decl(unionTypeCallSignatures7.ts, 8, 5))
25+
>f : Symbol(f, Decl(unionTypeCallSignatures7.ts, 7, 13))
26+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//// [tests/cases/conformance/types/union/unionTypeCallSignatures7.ts] ////
2+
3+
=== unionTypeCallSignatures7.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/55203
5+
6+
interface Callable<Name extends string> {
7+
(): `${Name} without id`;
8+
(id: number): `${Name} with id`;
9+
>id : number
10+
}
11+
12+
declare const f: Callable<"A"> | Callable<"B">;
13+
>f : Callable<"A"> | Callable<"B">
14+
15+
const result = f(123);
16+
>result : "A with id" | "B with id"
17+
>f(123) : "A with id" | "B with id"
18+
>f : Callable<"A"> | Callable<"B">
19+
>123 : 123
20+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
// https://github.com/microsoft/TypeScript/issues/55203
5+
6+
interface Callable<Name extends string> {
7+
(): `${Name} without id`;
8+
(id: number): `${Name} with id`;
9+
}
10+
11+
declare const f: Callable<"A"> | Callable<"B">;
12+
const result = f(123);

0 commit comments

Comments
 (0)