Skip to content

Commit e0311c8

Browse files
committed
Don't inferFromIndexTypes() twice
1 parent 70b902e commit e0311c8

File tree

7 files changed

+60
-524
lines changed

7 files changed

+60
-524
lines changed

src/compiler/checker.ts

+26-26
Original file line numberDiff line numberDiff line change
@@ -17922,6 +17922,32 @@ namespace ts {
1792217922
}
1792317923
// Infer from the members of source and target only if the two types are possibly related
1792417924
if (!typesDefinitelyUnrelated(source, target)) {
17925+
if (isArrayType(source) || isTupleType(source)) {
17926+
if (isTupleType(target)) {
17927+
const sourceLength = isTupleType(source) ? getLengthOfTupleType(source) : 0;
17928+
const targetLength = getLengthOfTupleType(target);
17929+
const sourceRestType = isTupleType(source) ? getRestTypeOfTupleType(source) : getElementTypeOfArrayType(source);
17930+
const targetRestType = getRestTypeOfTupleType(target);
17931+
const fixedLength = targetLength < sourceLength || sourceRestType ? targetLength : sourceLength;
17932+
for (let i = 0; i < fixedLength; i++) {
17933+
inferFromTypes(i < sourceLength ? getTypeArguments(<TypeReference>source)[i] : sourceRestType!, getTypeArguments(target)[i]);
17934+
}
17935+
if (targetRestType) {
17936+
const types = fixedLength < sourceLength ? getTypeArguments(<TypeReference>source).slice(fixedLength, sourceLength) : [];
17937+
if (sourceRestType) {
17938+
types.push(sourceRestType);
17939+
}
17940+
if (types.length) {
17941+
inferFromTypes(getUnionType(types), targetRestType);
17942+
}
17943+
}
17944+
return;
17945+
}
17946+
if (isArrayType(target)) {
17947+
inferFromIndexTypes(source, target);
17948+
return;
17949+
}
17950+
}
1792517951
inferFromProperties(source, target);
1792617952
inferFromSignatures(source, target, SignatureKind.Call);
1792717953
inferFromSignatures(source, target, SignatureKind.Construct);
@@ -17930,32 +17956,6 @@ namespace ts {
1793017956
}
1793117957

1793217958
function inferFromProperties(source: Type, target: Type) {
17933-
if (isArrayType(source) || isTupleType(source)) {
17934-
if (isTupleType(target)) {
17935-
const sourceLength = isTupleType(source) ? getLengthOfTupleType(source) : 0;
17936-
const targetLength = getLengthOfTupleType(target);
17937-
const sourceRestType = isTupleType(source) ? getRestTypeOfTupleType(source) : getElementTypeOfArrayType(source);
17938-
const targetRestType = getRestTypeOfTupleType(target);
17939-
const fixedLength = targetLength < sourceLength || sourceRestType ? targetLength : sourceLength;
17940-
for (let i = 0; i < fixedLength; i++) {
17941-
inferFromTypes(i < sourceLength ? getTypeArguments(<TypeReference>source)[i] : sourceRestType!, getTypeArguments(target)[i]);
17942-
}
17943-
if (targetRestType) {
17944-
const types = fixedLength < sourceLength ? getTypeArguments(<TypeReference>source).slice(fixedLength, sourceLength) : [];
17945-
if (sourceRestType) {
17946-
types.push(sourceRestType);
17947-
}
17948-
if (types.length) {
17949-
inferFromTypes(getUnionType(types), targetRestType);
17950-
}
17951-
}
17952-
return;
17953-
}
17954-
if (isArrayType(target)) {
17955-
inferFromIndexTypes(source, target);
17956-
return;
17957-
}
17958-
}
1795917959
const properties = getPropertiesOfObjectType(target);
1796017960
for (const targetProp of properties) {
1796117961
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Exit Code: 1
2+
Standard output:
3+
src/components/Hello.spec.ts(4,1): error TS2593: Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig.
4+
src/components/Hello.spec.ts(5,3): error TS2593: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig.
5+
src/components/Hello.spec.ts(14,5): error TS2304: Cannot find name 'expect'.
6+
7+
8+
9+
Standard error:

tests/baselines/reference/user/TypeScript-WeChat-Starter.log

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
Exit Code: 1
22
Standard output:
3-
../../../../../node_modules/@types/mocha/index.d.ts(2680,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'beforeEach' must be of type 'Lifecycle', but here has type 'HookFunction'.
4-
../../../../../node_modules/@types/mocha/index.d.ts(2698,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'afterEach' must be of type 'Lifecycle', but here has type 'HookFunction'.
5-
../../../../../node_modules/@types/mocha/index.d.ts(2714,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'describe' must be of type 'Describe', but here has type 'SuiteFunction'.
6-
../../../../../node_modules/@types/mocha/index.d.ts(2735,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'xdescribe' must be of type 'Describe', but here has type 'PendingSuiteFunction'.
7-
../../../../../node_modules/@types/mocha/index.d.ts(2749,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'it' must be of type 'It', but here has type 'TestFunction'.
8-
../../../../../node_modules/@types/mocha/index.d.ts(2763,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'test' must be of type 'It', but here has type 'TestFunction'.
9-
../../../../../node_modules/@types/mocha/index.d.ts(2770,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'xit' must be of type 'It', but here has type 'PendingTestFunction'.
3+
src/App.test.tsx(5,1): error TS2593: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig.
4+
src/components/Hello.test.tsx(5,1): error TS2593: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig.
5+
src/components/Hello.test.tsx(7,3): error TS2304: Cannot find name 'expect'.
6+
src/components/Hello.test.tsx(10,1): error TS2593: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig.
7+
src/components/Hello.test.tsx(12,3): error TS2304: Cannot find name 'expect'.
8+
src/components/Hello.test.tsx(15,1): error TS2593: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig.
9+
src/components/Hello.test.tsx(17,3): error TS2304: Cannot find name 'expect'.
10+
src/components/Hello.test.tsx(20,1): error TS2593: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig.
11+
src/components/Hello.test.tsx(21,3): error TS2304: Cannot find name 'expect'.
12+
src/components/Hello.test.tsx(26,1): error TS2593: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig.
13+
src/components/Hello.test.tsx(27,3): error TS2304: Cannot find name 'expect'.
1014

1115

1216

0 commit comments

Comments
 (0)