Skip to content

Commit f69901a

Browse files
committed
Don't inferFromIndexTypes() twice
1 parent 3b919e2 commit f69901a

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
@@ -18112,6 +18112,32 @@ namespace ts {
1811218112
}
1811318113
// Infer from the members of source and target only if the two types are possibly related
1811418114
if (!typesDefinitelyUnrelated(source, target)) {
18115+
if (isArrayType(source) || isTupleType(source)) {
18116+
if (isTupleType(target)) {
18117+
const sourceLength = isTupleType(source) ? getLengthOfTupleType(source) : 0;
18118+
const targetLength = getLengthOfTupleType(target);
18119+
const sourceRestType = isTupleType(source) ? getRestTypeOfTupleType(source) : getElementTypeOfArrayType(source);
18120+
const targetRestType = getRestTypeOfTupleType(target);
18121+
const fixedLength = targetLength < sourceLength || sourceRestType ? targetLength : sourceLength;
18122+
for (let i = 0; i < fixedLength; i++) {
18123+
inferFromTypes(i < sourceLength ? getTypeArguments(<TypeReference>source)[i] : sourceRestType!, getTypeArguments(target)[i]);
18124+
}
18125+
if (targetRestType) {
18126+
const types = fixedLength < sourceLength ? getTypeArguments(<TypeReference>source).slice(fixedLength, sourceLength) : [];
18127+
if (sourceRestType) {
18128+
types.push(sourceRestType);
18129+
}
18130+
if (types.length) {
18131+
inferFromTypes(getUnionType(types), targetRestType);
18132+
}
18133+
}
18134+
return;
18135+
}
18136+
if (isArrayType(target)) {
18137+
inferFromIndexTypes(source, target);
18138+
return;
18139+
}
18140+
}
1811518141
inferFromProperties(source, target);
1811618142
inferFromSignatures(source, target, SignatureKind.Call);
1811718143
inferFromSignatures(source, target, SignatureKind.Construct);
@@ -18120,32 +18146,6 @@ namespace ts {
1812018146
}
1812118147

1812218148
function inferFromProperties(source: Type, target: Type) {
18123-
if (isArrayType(source) || isTupleType(source)) {
18124-
if (isTupleType(target)) {
18125-
const sourceLength = isTupleType(source) ? getLengthOfTupleType(source) : 0;
18126-
const targetLength = getLengthOfTupleType(target);
18127-
const sourceRestType = isTupleType(source) ? getRestTypeOfTupleType(source) : getElementTypeOfArrayType(source);
18128-
const targetRestType = getRestTypeOfTupleType(target);
18129-
const fixedLength = targetLength < sourceLength || sourceRestType ? targetLength : sourceLength;
18130-
for (let i = 0; i < fixedLength; i++) {
18131-
inferFromTypes(i < sourceLength ? getTypeArguments(<TypeReference>source)[i] : sourceRestType!, getTypeArguments(target)[i]);
18132-
}
18133-
if (targetRestType) {
18134-
const types = fixedLength < sourceLength ? getTypeArguments(<TypeReference>source).slice(fixedLength, sourceLength) : [];
18135-
if (sourceRestType) {
18136-
types.push(sourceRestType);
18137-
}
18138-
if (types.length) {
18139-
inferFromTypes(getUnionType(types), targetRestType);
18140-
}
18141-
}
18142-
return;
18143-
}
18144-
if (isArrayType(target)) {
18145-
inferFromIndexTypes(source, target);
18146-
return;
18147-
}
18148-
}
1814918149
const properties = getPropertiesOfObjectType(target);
1815018150
for (const targetProp of properties) {
1815118151
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)