Skip to content

Commit ad05931

Browse files
committed
Don't inferFromIndexTypes() twice
1 parent d2c5d54 commit ad05931

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

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