Skip to content

Commit adfa351

Browse files
committed
Don't inferFromIndexTypes() twice
1 parent 7726464 commit adfa351

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
@@ -18301,6 +18301,32 @@ namespace ts {
1830118301
}
1830218302
// Infer from the members of source and target only if the two types are possibly related
1830318303
if (!typesDefinitelyUnrelated(source, target)) {
18304+
if (isArrayType(source) || isTupleType(source)) {
18305+
if (isTupleType(target)) {
18306+
const sourceLength = isTupleType(source) ? getLengthOfTupleType(source) : 0;
18307+
const targetLength = getLengthOfTupleType(target);
18308+
const sourceRestType = isTupleType(source) ? getRestTypeOfTupleType(source) : getElementTypeOfArrayType(source);
18309+
const targetRestType = getRestTypeOfTupleType(target);
18310+
const fixedLength = targetLength < sourceLength || sourceRestType ? targetLength : sourceLength;
18311+
for (let i = 0; i < fixedLength; i++) {
18312+
inferFromTypes(i < sourceLength ? getTypeArguments(<TypeReference>source)[i] : sourceRestType!, getTypeArguments(target)[i]);
18313+
}
18314+
if (targetRestType) {
18315+
const types = fixedLength < sourceLength ? getTypeArguments(<TypeReference>source).slice(fixedLength, sourceLength) : [];
18316+
if (sourceRestType) {
18317+
types.push(sourceRestType);
18318+
}
18319+
if (types.length) {
18320+
inferFromTypes(getUnionType(types), targetRestType);
18321+
}
18322+
}
18323+
return;
18324+
}
18325+
if (isArrayType(target)) {
18326+
inferFromIndexTypes(source, target);
18327+
return;
18328+
}
18329+
}
1830418330
inferFromProperties(source, target);
1830518331
inferFromSignatures(source, target, SignatureKind.Call);
1830618332
inferFromSignatures(source, target, SignatureKind.Construct);
@@ -18309,32 +18335,6 @@ namespace ts {
1830918335
}
1831018336

1831118337
function inferFromProperties(source: Type, target: Type) {
18312-
if (isArrayType(source) || isTupleType(source)) {
18313-
if (isTupleType(target)) {
18314-
const sourceLength = isTupleType(source) ? getLengthOfTupleType(source) : 0;
18315-
const targetLength = getLengthOfTupleType(target);
18316-
const sourceRestType = isTupleType(source) ? getRestTypeOfTupleType(source) : getElementTypeOfArrayType(source);
18317-
const targetRestType = getRestTypeOfTupleType(target);
18318-
const fixedLength = targetLength < sourceLength || sourceRestType ? targetLength : sourceLength;
18319-
for (let i = 0; i < fixedLength; i++) {
18320-
inferFromTypes(i < sourceLength ? getTypeArguments(<TypeReference>source)[i] : sourceRestType!, getTypeArguments(target)[i]);
18321-
}
18322-
if (targetRestType) {
18323-
const types = fixedLength < sourceLength ? getTypeArguments(<TypeReference>source).slice(fixedLength, sourceLength) : [];
18324-
if (sourceRestType) {
18325-
types.push(sourceRestType);
18326-
}
18327-
if (types.length) {
18328-
inferFromTypes(getUnionType(types), targetRestType);
18329-
}
18330-
}
18331-
return;
18332-
}
18333-
if (isArrayType(target)) {
18334-
inferFromIndexTypes(source, target);
18335-
return;
18336-
}
18337-
}
1833818338
const properties = getPropertiesOfObjectType(target);
1833918339
for (const targetProp of properties) {
1834018340
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)