diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 9f77641faf5cc..5c0416163ea00 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -27211,7 +27211,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { isFunctionLike(node) && !getImmediatelyInvokedFunctionExpression(node) || node.kind === SyntaxKind.ModuleBlock || node.kind === SyntaxKind.SourceFile || - node.kind === SyntaxKind.CatchClause || node.kind === SyntaxKind.PropertyDeclaration)!; } @@ -27587,7 +27586,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { const isParameter = getRootDeclaration(declaration).kind === SyntaxKind.Parameter; const declarationContainer = getControlFlowContainer(declaration); let flowContainer = getControlFlowContainer(node); - const isCatch = flowContainer.kind === SyntaxKind.CatchClause; const isOuterVariable = flowContainer !== declarationContainer; const isSpreadDestructuringAssignmentTarget = node.parent && node.parent.parent && isSpreadAssignment(node.parent) && isDestructuringAssignmentTarget(node.parent.parent); const isModuleExports = symbol.flags & SymbolFlags.ModuleExports; @@ -27602,7 +27600,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { // We only look for uninitialized variables in strict null checking mode, and only when we can analyze // the entire control flow graph from the variable's declaration (i.e. when the flow container and // declaration container are the same). - const assumeInitialized = isParameter || isCatch || isAlias || isOuterVariable || isSpreadDestructuringAssignmentTarget || isModuleExports || isSameScopedBindingElement(node, declaration) || + const assumeInitialized = isParameter || isAlias || isOuterVariable || isSpreadDestructuringAssignmentTarget || isModuleExports || isSameScopedBindingElement(node, declaration) || type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & (TypeFlags.AnyOrUnknown | TypeFlags.Void)) !== 0 || isInTypeQuery(node) || isInAmbientOrTypeNode(node) || node.parent.kind === SyntaxKind.ExportSpecifier) || node.parent.kind === SyntaxKind.NonNullExpression || diff --git a/tests/baselines/reference/potentiallyUnassignedVariableInCatch.js b/tests/baselines/reference/potentiallyUnassignedVariableInCatch.js new file mode 100644 index 0000000000000..16cf0e40c238a --- /dev/null +++ b/tests/baselines/reference/potentiallyUnassignedVariableInCatch.js @@ -0,0 +1,22 @@ +//// [potentiallyUnassignedVariableInCatch.ts] +let foo; +try { + if (Math.random() > 0.5) { + foo = 1234; + } +} catch { + foo; +} + + +//// [potentiallyUnassignedVariableInCatch.js] +"use strict"; +var foo; +try { + if (Math.random() > 0.5) { + foo = 1234; + } +} +catch (_a) { + foo; +} diff --git a/tests/baselines/reference/potentiallyUnassignedVariableInCatch.symbols b/tests/baselines/reference/potentiallyUnassignedVariableInCatch.symbols new file mode 100644 index 0000000000000..97040ebb5c423 --- /dev/null +++ b/tests/baselines/reference/potentiallyUnassignedVariableInCatch.symbols @@ -0,0 +1,18 @@ +=== tests/cases/compiler/potentiallyUnassignedVariableInCatch.ts === +let foo; +>foo : Symbol(foo, Decl(potentiallyUnassignedVariableInCatch.ts, 0, 3)) + +try { + if (Math.random() > 0.5) { +>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) +>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) + + foo = 1234; +>foo : Symbol(foo, Decl(potentiallyUnassignedVariableInCatch.ts, 0, 3)) + } +} catch { + foo; +>foo : Symbol(foo, Decl(potentiallyUnassignedVariableInCatch.ts, 0, 3)) +} + diff --git a/tests/baselines/reference/potentiallyUnassignedVariableInCatch.types b/tests/baselines/reference/potentiallyUnassignedVariableInCatch.types new file mode 100644 index 0000000000000..ff381216aa3da --- /dev/null +++ b/tests/baselines/reference/potentiallyUnassignedVariableInCatch.types @@ -0,0 +1,23 @@ +=== tests/cases/compiler/potentiallyUnassignedVariableInCatch.ts === +let foo; +>foo : any + +try { + if (Math.random() > 0.5) { +>Math.random() > 0.5 : boolean +>Math.random() : number +>Math.random : () => number +>Math : Math +>random : () => number +>0.5 : 0.5 + + foo = 1234; +>foo = 1234 : 1234 +>foo : any +>1234 : 1234 + } +} catch { + foo; +>foo : number | undefined +} + diff --git a/tests/cases/compiler/potentiallyUnassignedVariableInCatch.ts b/tests/cases/compiler/potentiallyUnassignedVariableInCatch.ts new file mode 100644 index 0000000000000..8f1d4d26bf394 --- /dev/null +++ b/tests/cases/compiler/potentiallyUnassignedVariableInCatch.ts @@ -0,0 +1,10 @@ +// @strict: true + +let foo; +try { + if (Math.random() > 0.5) { + foo = 1234; + } +} catch { + foo; +}