From 759537c5b22ed7f50dc633cd4012511d84a116a3 Mon Sep 17 00:00:00 2001 From: Jack Bates Date: Tue, 15 Oct 2019 13:29:19 -0700 Subject: [PATCH] Don't inferFromIndexTypes() twice --- src/compiler/checker.ts | 52 +++++++++---------- .../reference/restTupleElements1.types | 2 +- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 85c6b695063d7..b99bf712ad7d2 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -17921,6 +17921,32 @@ namespace ts { } // Infer from the members of source and target only if the two types are possibly related if (!typesDefinitelyUnrelated(source, target)) { + if (isArrayType(source) || isTupleType(source)) { + if (isTupleType(target)) { + const sourceLength = isTupleType(source) ? getLengthOfTupleType(source) : 0; + const targetLength = getLengthOfTupleType(target); + const sourceRestType = isTupleType(source) ? getRestTypeOfTupleType(source) : getElementTypeOfArrayType(source); + const targetRestType = getRestTypeOfTupleType(target); + const fixedLength = targetLength < sourceLength || sourceRestType ? targetLength : sourceLength; + for (let i = 0; i < fixedLength; i++) { + inferFromTypes(i < sourceLength ? getTypeArguments(source)[i] : sourceRestType!, getTypeArguments(target)[i]); + } + if (targetRestType) { + const types = fixedLength < sourceLength ? getTypeArguments(source).slice(fixedLength, sourceLength) : []; + if (sourceRestType) { + types.push(sourceRestType); + } + if (types.length) { + inferFromTypes(getUnionType(types), targetRestType); + } + } + return; + } + if (isArrayType(target)) { + inferFromIndexTypes(source, target); + return; + } + } inferFromProperties(source, target); inferFromSignatures(source, target, SignatureKind.Call); inferFromSignatures(source, target, SignatureKind.Construct); @@ -17929,32 +17955,6 @@ namespace ts { } function inferFromProperties(source: Type, target: Type) { - if (isArrayType(source) || isTupleType(source)) { - if (isTupleType(target)) { - const sourceLength = isTupleType(source) ? getLengthOfTupleType(source) : 0; - const targetLength = getLengthOfTupleType(target); - const sourceRestType = isTupleType(source) ? getRestTypeOfTupleType(source) : getElementTypeOfArrayType(source); - const targetRestType = getRestTypeOfTupleType(target); - const fixedLength = targetLength < sourceLength || sourceRestType ? targetLength : sourceLength; - for (let i = 0; i < fixedLength; i++) { - inferFromTypes(i < sourceLength ? getTypeArguments(source)[i] : sourceRestType!, getTypeArguments(target)[i]); - } - if (targetRestType) { - const types = fixedLength < sourceLength ? getTypeArguments(source).slice(fixedLength, sourceLength) : []; - if (sourceRestType) { - types.push(sourceRestType); - } - if (types.length) { - inferFromTypes(getUnionType(types), targetRestType); - } - } - return; - } - if (isArrayType(target)) { - inferFromIndexTypes(source, target); - return; - } - } const properties = getPropertiesOfObjectType(target); for (const targetProp of properties) { const sourceProp = getPropertyOfType(source, targetProp.escapedName); diff --git a/tests/baselines/reference/restTupleElements1.types b/tests/baselines/reference/restTupleElements1.types index 88c98395f21c3..3eb0b00087072 100644 --- a/tests/baselines/reference/restTupleElements1.types +++ b/tests/baselines/reference/restTupleElements1.types @@ -173,7 +173,7 @@ f0([]); // Error >[] : never[] f0([1]); ->f0([1]) : [number, number] +>f0([1]) : [number, unknown] >f0 : (x: [T, ...U[]]) => [T, U] >[1] : [number] >1 : 1