Skip to content

Commit 759537c

Browse files
committed
Don't inferFromIndexTypes() twice
1 parent ea4e6b3 commit 759537c

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

src/compiler/checker.ts

+26-26
Original file line numberDiff line numberDiff line change
@@ -17921,6 +17921,32 @@ namespace ts {
1792117921
}
1792217922
// Infer from the members of source and target only if the two types are possibly related
1792317923
if (!typesDefinitelyUnrelated(source, target)) {
17924+
if (isArrayType(source) || isTupleType(source)) {
17925+
if (isTupleType(target)) {
17926+
const sourceLength = isTupleType(source) ? getLengthOfTupleType(source) : 0;
17927+
const targetLength = getLengthOfTupleType(target);
17928+
const sourceRestType = isTupleType(source) ? getRestTypeOfTupleType(source) : getElementTypeOfArrayType(source);
17929+
const targetRestType = getRestTypeOfTupleType(target);
17930+
const fixedLength = targetLength < sourceLength || sourceRestType ? targetLength : sourceLength;
17931+
for (let i = 0; i < fixedLength; i++) {
17932+
inferFromTypes(i < sourceLength ? getTypeArguments(<TypeReference>source)[i] : sourceRestType!, getTypeArguments(target)[i]);
17933+
}
17934+
if (targetRestType) {
17935+
const types = fixedLength < sourceLength ? getTypeArguments(<TypeReference>source).slice(fixedLength, sourceLength) : [];
17936+
if (sourceRestType) {
17937+
types.push(sourceRestType);
17938+
}
17939+
if (types.length) {
17940+
inferFromTypes(getUnionType(types), targetRestType);
17941+
}
17942+
}
17943+
return;
17944+
}
17945+
if (isArrayType(target)) {
17946+
inferFromIndexTypes(source, target);
17947+
return;
17948+
}
17949+
}
1792417950
inferFromProperties(source, target);
1792517951
inferFromSignatures(source, target, SignatureKind.Call);
1792617952
inferFromSignatures(source, target, SignatureKind.Construct);
@@ -17929,32 +17955,6 @@ namespace ts {
1792917955
}
1793017956

1793117957
function inferFromProperties(source: Type, target: Type) {
17932-
if (isArrayType(source) || isTupleType(source)) {
17933-
if (isTupleType(target)) {
17934-
const sourceLength = isTupleType(source) ? getLengthOfTupleType(source) : 0;
17935-
const targetLength = getLengthOfTupleType(target);
17936-
const sourceRestType = isTupleType(source) ? getRestTypeOfTupleType(source) : getElementTypeOfArrayType(source);
17937-
const targetRestType = getRestTypeOfTupleType(target);
17938-
const fixedLength = targetLength < sourceLength || sourceRestType ? targetLength : sourceLength;
17939-
for (let i = 0; i < fixedLength; i++) {
17940-
inferFromTypes(i < sourceLength ? getTypeArguments(<TypeReference>source)[i] : sourceRestType!, getTypeArguments(target)[i]);
17941-
}
17942-
if (targetRestType) {
17943-
const types = fixedLength < sourceLength ? getTypeArguments(<TypeReference>source).slice(fixedLength, sourceLength) : [];
17944-
if (sourceRestType) {
17945-
types.push(sourceRestType);
17946-
}
17947-
if (types.length) {
17948-
inferFromTypes(getUnionType(types), targetRestType);
17949-
}
17950-
}
17951-
return;
17952-
}
17953-
if (isArrayType(target)) {
17954-
inferFromIndexTypes(source, target);
17955-
return;
17956-
}
17957-
}
1795817958
const properties = getPropertiesOfObjectType(target);
1795917959
for (const targetProp of properties) {
1796017960
const sourceProp = getPropertyOfType(source, targetProp.escapedName);

tests/baselines/reference/restTupleElements1.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ f0([]); // Error
173173
>[] : never[]
174174

175175
f0([1]);
176-
>f0([1]) : [number, number]
176+
>f0([1]) : [number, unknown]
177177
>f0 : <T, U>(x: [T, ...U[]]) => [T, U]
178178
>[1] : [number]
179179
>1 : 1

0 commit comments

Comments
 (0)