@@ -25969,6 +25969,8 @@ namespace ts {
25969
25969
const isOuterVariable = flowContainer !== declarationContainer;
25970
25970
const isSpreadDestructuringAssignmentTarget = node.parent && node.parent.parent && isSpreadAssignment(node.parent) && isDestructuringAssignmentTarget(node.parent.parent);
25971
25971
const isModuleExports = symbol.flags & SymbolFlags.ModuleExports;
25972
+ const typeIsAutomatic = type === autoType || type === autoArrayType;
25973
+ const isAutomaticTypeInNonNull = typeIsAutomatic && node.parent.kind === SyntaxKind.NonNullExpression;
25972
25974
// When the control flow originates in a function expression or arrow function and we are referencing
25973
25975
// a const variable or parameter from an outer function, we extend the origin of the control flow
25974
25976
// analysis to include the immediately enclosing function.
@@ -25986,27 +25988,15 @@ namespace ts {
25986
25988
node.parent.kind === SyntaxKind.NonNullExpression ||
25987
25989
declaration.kind === SyntaxKind.VariableDeclaration && (declaration as VariableDeclaration).exclamationToken ||
25988
25990
declaration.flags & NodeFlags.Ambient;
25989
-
25990
- // TODO: refactor this
25991
- // when non null and an automatic type are involved, we should get the flow type and then remove optionality
25992
- const createFlowType = (initialType: Type) => getFlowTypeOfReference(node, type, initialType, flowContainer);
25993
- let flowType: Type | undefined;
25994
- if (node.parent.kind === SyntaxKind.NonNullExpression && (type === autoType || type === autoArrayType)) {
25995
- const initialType = undefinedType;
25996
- flowType = createFlowType(initialType);
25997
- flowType = getNonNullableType(flowType);
25998
- }
25999
- else {
26000
- const initialType = assumeInitialized ? (isParameter ? removeOptionalityFromDeclaredType(type, declaration as VariableLikeDeclaration) : type) :
26001
- type === autoType || type === autoArrayType ? undefinedType :
26002
- getOptionalType(type);
26003
- flowType = createFlowType(initialType);
26004
- }
26005
-
25991
+ const initialType = isAutomaticTypeInNonNull ? undefinedType :
25992
+ assumeInitialized ? (isParameter ? removeOptionalityFromDeclaredType(type, declaration as VariableLikeDeclaration) : type) :
25993
+ typeIsAutomatic ? undefinedType : getOptionalType(type);
25994
+ const flowType = isAutomaticTypeInNonNull ? getNonNullableType(getFlowTypeOfReference(node, type, initialType, flowContainer)) :
25995
+ getFlowTypeOfReference(node, type, initialType, flowContainer);
26006
25996
// A variable is considered uninitialized when it is possible to analyze the entire control flow graph
26007
25997
// from declaration to use, and when the variable's declared type doesn't include undefined but the
26008
25998
// control flow based type does include undefined.
26009
- if (!isEvolvingArrayOperationTarget(node) && (type === autoType || type === autoArrayType) ) {
25999
+ if (!isEvolvingArrayOperationTarget(node) && typeIsAutomatic ) {
26010
26000
if (flowType === autoType || flowType === autoArrayType) {
26011
26001
if (noImplicitAny) {
26012
26002
error(getNameOfDeclaration(declaration), Diagnostics.Variable_0_implicitly_has_type_1_in_some_locations_where_its_type_cannot_be_determined, symbolToString(symbol), typeToString(flowType));
0 commit comments