Skip to content

Commit 6ebe089

Browse files
committed
Don't inferFromIndexTypes() twice
1 parent 9a357c1 commit 6ebe089

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
@@ -18269,6 +18269,32 @@ namespace ts {
1826918269
}
1827018270
// Infer from the members of source and target only if the two types are possibly related
1827118271
if (!typesDefinitelyUnrelated(source, target)) {
18272+
if (isArrayType(source) || isTupleType(source)) {
18273+
if (isTupleType(target)) {
18274+
const sourceLength = isTupleType(source) ? getLengthOfTupleType(source) : 0;
18275+
const targetLength = getLengthOfTupleType(target);
18276+
const sourceRestType = isTupleType(source) ? getRestTypeOfTupleType(source) : getElementTypeOfArrayType(source);
18277+
const targetRestType = getRestTypeOfTupleType(target);
18278+
const fixedLength = targetLength < sourceLength || sourceRestType ? targetLength : sourceLength;
18279+
for (let i = 0; i < fixedLength; i++) {
18280+
inferFromTypes(i < sourceLength ? getTypeArguments(<TypeReference>source)[i] : sourceRestType!, getTypeArguments(target)[i]);
18281+
}
18282+
if (targetRestType) {
18283+
const types = fixedLength < sourceLength ? getTypeArguments(<TypeReference>source).slice(fixedLength, sourceLength) : [];
18284+
if (sourceRestType) {
18285+
types.push(sourceRestType);
18286+
}
18287+
if (types.length) {
18288+
inferFromTypes(getUnionType(types), targetRestType);
18289+
}
18290+
}
18291+
return;
18292+
}
18293+
if (isArrayType(target)) {
18294+
inferFromIndexTypes(source, target);
18295+
return;
18296+
}
18297+
}
1827218298
inferFromProperties(source, target);
1827318299
inferFromSignatures(source, target, SignatureKind.Call);
1827418300
inferFromSignatures(source, target, SignatureKind.Construct);
@@ -18277,32 +18303,6 @@ namespace ts {
1827718303
}
1827818304

1827918305
function inferFromProperties(source: Type, target: Type) {
18280-
if (isArrayType(source) || isTupleType(source)) {
18281-
if (isTupleType(target)) {
18282-
const sourceLength = isTupleType(source) ? getLengthOfTupleType(source) : 0;
18283-
const targetLength = getLengthOfTupleType(target);
18284-
const sourceRestType = isTupleType(source) ? getRestTypeOfTupleType(source) : getElementTypeOfArrayType(source);
18285-
const targetRestType = getRestTypeOfTupleType(target);
18286-
const fixedLength = targetLength < sourceLength || sourceRestType ? targetLength : sourceLength;
18287-
for (let i = 0; i < fixedLength; i++) {
18288-
inferFromTypes(i < sourceLength ? getTypeArguments(<TypeReference>source)[i] : sourceRestType!, getTypeArguments(target)[i]);
18289-
}
18290-
if (targetRestType) {
18291-
const types = fixedLength < sourceLength ? getTypeArguments(<TypeReference>source).slice(fixedLength, sourceLength) : [];
18292-
if (sourceRestType) {
18293-
types.push(sourceRestType);
18294-
}
18295-
if (types.length) {
18296-
inferFromTypes(getUnionType(types), targetRestType);
18297-
}
18298-
}
18299-
return;
18300-
}
18301-
if (isArrayType(target)) {
18302-
inferFromIndexTypes(source, target);
18303-
return;
18304-
}
18305-
}
1830618306
const properties = getPropertiesOfObjectType(target);
1830718307
for (const targetProp of properties) {
1830818308
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)