Skip to content

Commit a5189b1

Browse files
committed
refactor: refactor the fix
1 parent 77f5f81 commit a5189b1

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25969,6 +25969,8 @@ namespace ts {
2596925969
const isOuterVariable = flowContainer !== declarationContainer;
2597025970
const isSpreadDestructuringAssignmentTarget = node.parent && node.parent.parent && isSpreadAssignment(node.parent) && isDestructuringAssignmentTarget(node.parent.parent);
2597125971
const isModuleExports = symbol.flags & SymbolFlags.ModuleExports;
25972+
const typeIsAutomatic = type === autoType || type === autoArrayType;
25973+
const isAutomaticTypeInNonNull = typeIsAutomatic && node.parent.kind === SyntaxKind.NonNullExpression;
2597225974
// When the control flow originates in a function expression or arrow function and we are referencing
2597325975
// a const variable or parameter from an outer function, we extend the origin of the control flow
2597425976
// analysis to include the immediately enclosing function.
@@ -25986,27 +25988,15 @@ namespace ts {
2598625988
node.parent.kind === SyntaxKind.NonNullExpression ||
2598725989
declaration.kind === SyntaxKind.VariableDeclaration && (declaration as VariableDeclaration).exclamationToken ||
2598825990
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);
2600625996
// A variable is considered uninitialized when it is possible to analyze the entire control flow graph
2600725997
// from declaration to use, and when the variable's declared type doesn't include undefined but the
2600825998
// control flow based type does include undefined.
26009-
if (!isEvolvingArrayOperationTarget(node) && (type === autoType || type === autoArrayType)) {
25999+
if (!isEvolvingArrayOperationTarget(node) && typeIsAutomatic) {
2601026000
if (flowType === autoType || flowType === autoArrayType) {
2601126001
if (noImplicitAny) {
2601226002
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

Comments
 (0)