Skip to content

Commit c5b47fb

Browse files
committed
Correct partial signature matching
1 parent 067e1cc commit c5b47fb

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

src/compiler/checker.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5603,18 +5603,29 @@ namespace ts {
56035603
return compareTypes(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp));
56045604
}
56055605

5606+
// A source signature matches a target signature if the two signatures have the same number of required,
5607+
// optional, and rest parameters.
5608+
function isMatchingSignature(source: Signature, target: Signature) {
5609+
return source.parameters.length === target.parameters.length &&
5610+
source.minArgumentCount === target.minArgumentCount &&
5611+
source.hasRestParameter === target.hasRestParameter;
5612+
}
5613+
5614+
// A source signature partially matches a target signature if the target signature has no fewer required
5615+
// parameters and no more overall parameters than the source signature (where a signature with a rest
5616+
// parameter is always considered to have more overall parameters than one without).
5617+
function isPartiallyMatchingSignature(source: Signature, target: Signature) {
5618+
return source.minArgumentCount <= target.minArgumentCount && (
5619+
source.hasRestParameter && !target.hasRestParameter ||
5620+
source.hasRestParameter === target.hasRestParameter && source.parameters.length >= target.parameters.length);
5621+
}
5622+
56065623
function compareSignatures(source: Signature, target: Signature, partialMatch: boolean, ignoreReturnTypes: boolean, compareTypes: (s: Type, t: Type) => Ternary): Ternary {
56075624
if (source === target) {
56085625
return Ternary.True;
56095626
}
5610-
if (source.parameters.length !== target.parameters.length ||
5611-
source.minArgumentCount !== target.minArgumentCount ||
5612-
source.hasRestParameter !== target.hasRestParameter) {
5613-
if (!partialMatch ||
5614-
source.parameters.length < target.parameters.length && !source.hasRestParameter ||
5615-
source.minArgumentCount > target.minArgumentCount) {
5616-
return Ternary.False;
5617-
}
5627+
if (!(isMatchingSignature(source, target) || partialMatch && isPartiallyMatchingSignature(source, target))) {
5628+
return Ternary.False;
56185629
}
56195630
let result = Ternary.True;
56205631
if (source.typeParameters && target.typeParameters) {

0 commit comments

Comments
 (0)