Skip to content

Commit 2e46219

Browse files
committed
Don't inferFromIndexTypes() twice
1 parent 6f079a4 commit 2e46219

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
@@ -18373,6 +18373,32 @@ namespace ts {
1837318373
}
1837418374
// Infer from the members of source and target only if the two types are possibly related
1837518375
if (!typesDefinitelyUnrelated(source, target)) {
18376+
if (isArrayType(source) || isTupleType(source)) {
18377+
if (isTupleType(target)) {
18378+
const sourceLength = isTupleType(source) ? getLengthOfTupleType(source) : 0;
18379+
const targetLength = getLengthOfTupleType(target);
18380+
const sourceRestType = isTupleType(source) ? getRestTypeOfTupleType(source) : getElementTypeOfArrayType(source);
18381+
const targetRestType = getRestTypeOfTupleType(target);
18382+
const fixedLength = targetLength < sourceLength || sourceRestType ? targetLength : sourceLength;
18383+
for (let i = 0; i < fixedLength; i++) {
18384+
inferFromTypes(i < sourceLength ? getTypeArguments(<TypeReference>source)[i] : sourceRestType!, getTypeArguments(target)[i]);
18385+
}
18386+
if (targetRestType) {
18387+
const types = fixedLength < sourceLength ? getTypeArguments(<TypeReference>source).slice(fixedLength, sourceLength) : [];
18388+
if (sourceRestType) {
18389+
types.push(sourceRestType);
18390+
}
18391+
if (types.length) {
18392+
inferFromTypes(getUnionType(types), targetRestType);
18393+
}
18394+
}
18395+
return;
18396+
}
18397+
if (isArrayType(target)) {
18398+
inferFromIndexTypes(source, target);
18399+
return;
18400+
}
18401+
}
1837618402
inferFromProperties(source, target);
1837718403
inferFromSignatures(source, target, SignatureKind.Call);
1837818404
inferFromSignatures(source, target, SignatureKind.Construct);
@@ -18381,32 +18407,6 @@ namespace ts {
1838118407
}
1838218408

1838318409
function inferFromProperties(source: Type, target: Type) {
18384-
if (isArrayType(source) || isTupleType(source)) {
18385-
if (isTupleType(target)) {
18386-
const sourceLength = isTupleType(source) ? getLengthOfTupleType(source) : 0;
18387-
const targetLength = getLengthOfTupleType(target);
18388-
const sourceRestType = isTupleType(source) ? getRestTypeOfTupleType(source) : getElementTypeOfArrayType(source);
18389-
const targetRestType = getRestTypeOfTupleType(target);
18390-
const fixedLength = targetLength < sourceLength || sourceRestType ? targetLength : sourceLength;
18391-
for (let i = 0; i < fixedLength; i++) {
18392-
inferFromTypes(i < sourceLength ? getTypeArguments(<TypeReference>source)[i] : sourceRestType!, getTypeArguments(target)[i]);
18393-
}
18394-
if (targetRestType) {
18395-
const types = fixedLength < sourceLength ? getTypeArguments(<TypeReference>source).slice(fixedLength, sourceLength) : [];
18396-
if (sourceRestType) {
18397-
types.push(sourceRestType);
18398-
}
18399-
if (types.length) {
18400-
inferFromTypes(getUnionType(types), targetRestType);
18401-
}
18402-
}
18403-
return;
18404-
}
18405-
if (isArrayType(target)) {
18406-
inferFromIndexTypes(source, target);
18407-
return;
18408-
}
18409-
}
1841018410
const properties = getPropertiesOfObjectType(target);
1841118411
for (const targetProp of properties) {
1841218412
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
>[] : []
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)