Skip to content

Commit f5f6133

Browse files
committed
Revise complex rest parameter handling in relations and inference
1 parent 4e44b08 commit f5f6133

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10723,8 +10723,9 @@ namespace ts {
1072310723

1072410724
const sourceCount = getParameterCount(source);
1072510725
const sourceRestType = getNonArrayRestType(source);
10726-
const targetRestType = sourceRestType ? getNonArrayRestType(target) : undefined;
10727-
if (sourceRestType && !(targetRestType && sourceCount === targetCount)) {
10726+
const targetRestType = getNonArrayRestType(target);
10727+
if (sourceRestType && targetRestType && sourceCount !== targetCount) {
10728+
// We're not able to relate misaliged complex rest parameters
1072810729
return Ternary.False;
1072910730
}
1073010731

@@ -10750,11 +10751,12 @@ namespace ts {
1075010751
}
1075110752
}
1075210753

10753-
const paramCount = Math.max(sourceCount, targetCount);
10754-
const lastIndex = paramCount - 1;
10754+
const paramCount = sourceRestType || targetRestType ? Math.min(sourceCount, targetCount) : Math.max(sourceCount, targetCount);
10755+
const restIndex = sourceRestType || targetRestType ? paramCount - 1 : -1;
10756+
1075510757
for (let i = 0; i < paramCount; i++) {
10756-
const sourceType = i === lastIndex && sourceRestType || getTypeAtPosition(source, i);
10757-
const targetType = i === lastIndex && targetRestType || getTypeAtPosition(target, i);
10758+
const sourceType = i === restIndex ? getRestTypeAtPosition(source, i) : getTypeAtPosition(source, i);
10759+
const targetType = i === restIndex ? getRestTypeAtPosition(target, i) : getTypeAtPosition(target, i);
1075810760
// In order to ensure that any generic type Foo<T> is at least co-variant with respect to T no matter
1075910761
// how Foo uses T, we need to relate parameters bi-variantly (given that parameters are input positions,
1076010762
// they naturally relate only contra-variantly). However, if the source and target parameters both have
@@ -13049,11 +13051,9 @@ namespace ts {
1304913051
const targetCount = getParameterCount(target);
1305013052
const sourceRestType = getEffectiveRestType(source);
1305113053
const targetRestType = getEffectiveRestType(target);
13052-
const maxCount = sourceRestType && targetRestType ? Math.max(sourceCount, targetCount) :
13054+
const paramCount = targetRestType ? Math.min(targetCount - 1, sourceCount) :
1305313055
sourceRestType ? targetCount :
13054-
targetRestType ? sourceCount :
1305513056
Math.min(sourceCount, targetCount);
13056-
const paramCount = targetRestType ? Math.min(targetCount - 1, maxCount) : maxCount;
1305713057
for (let i = 0; i < paramCount; i++) {
1305813058
callback(getTypeAtPosition(source, i), getTypeAtPosition(target, i));
1305913059
}

0 commit comments

Comments
 (0)