mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Merge pull request #8556 from Microsoft/controlFlowLoopAnalysis
Fix control flow loop analysis issue
This commit is contained in:
@@ -7780,16 +7780,15 @@ namespace ts {
|
||||
if (cache[key]) {
|
||||
return cache[key];
|
||||
}
|
||||
// If the type at a particular antecedent path is the declared type and the
|
||||
// reference is known to always be assigned (i.e. when declared and initial types
|
||||
// are the same), there is no reason to process more antecedents since the only
|
||||
// possible outcome is subtypes that will be removed in the final union type anyway.
|
||||
if (type === declaredType && declaredType === initialType) {
|
||||
return cache[key] = type;
|
||||
}
|
||||
if (!contains(antecedentTypes, type)) {
|
||||
antecedentTypes.push(type);
|
||||
}
|
||||
// If the type at a particular antecedent path is the declared type there is no
|
||||
// reason to process more antecedents since the only possible outcome is subtypes
|
||||
// that will be removed in the final union type anyway.
|
||||
if (type === declaredType) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return cache[key] = getUnionType(antecedentTypes);
|
||||
}
|
||||
|
||||
@@ -36,4 +36,23 @@ tests/cases/compiler/controlFlowLoopAnalysis.ts(13,25): error TS2345: Argument o
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Repro from #8511
|
||||
|
||||
function mapUntilCant<a, b>(
|
||||
values: a[],
|
||||
canTake: (value: a, index: number) => boolean,
|
||||
mapping: (value: a, index: number) => b
|
||||
): b[] {
|
||||
let result: b[] = [];
|
||||
for (let index = 0, length = values.length; index < length; index++) {
|
||||
let value = values[index];
|
||||
if (canTake(value, index)) {
|
||||
result.push(mapping(value, index));
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -29,6 +29,25 @@ function test2() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Repro from #8511
|
||||
|
||||
function mapUntilCant<a, b>(
|
||||
values: a[],
|
||||
canTake: (value: a, index: number) => boolean,
|
||||
mapping: (value: a, index: number) => b
|
||||
): b[] {
|
||||
let result: b[] = [];
|
||||
for (let index = 0, length = values.length; index < length; index++) {
|
||||
let value = values[index];
|
||||
if (canTake(value, index)) {
|
||||
result.push(mapping(value, index));
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
//// [controlFlowLoopAnalysis.js]
|
||||
@@ -56,3 +75,17 @@ function test2() {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Repro from #8511
|
||||
function mapUntilCant(values, canTake, mapping) {
|
||||
var result = [];
|
||||
for (var index = 0, length = values.length; index < length; index++) {
|
||||
var value = values[index];
|
||||
if (canTake(value, index)) {
|
||||
result.push(mapping(value, index));
|
||||
}
|
||||
else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
// @strictNullChecks: true
|
||||
// @noImplicitAny: true
|
||||
|
||||
// Repro from #8418
|
||||
|
||||
@@ -29,3 +30,22 @@ function test2() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Repro from #8511
|
||||
|
||||
function mapUntilCant<a, b>(
|
||||
values: a[],
|
||||
canTake: (value: a, index: number) => boolean,
|
||||
mapping: (value: a, index: number) => b
|
||||
): b[] {
|
||||
let result: b[] = [];
|
||||
for (let index = 0, length = values.length; index < length; index++) {
|
||||
let value = values[index];
|
||||
if (canTake(value, index)) {
|
||||
result.push(mapping(value, index));
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user