Skip to content

Commit 475e201

Browse files
Add new 'useUnknownInCatchVariables' flag.
1 parent 67b4b90 commit 475e201

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8603,7 +8603,7 @@ namespace ts {
86038603
const declaration = symbol.valueDeclaration;
86048604
if (isCatchClauseVariableDeclarationOrBindingElement(declaration)) {
86058605
const decl = declaration as VariableDeclaration;
8606-
if (!decl.type) return anyType;
8606+
if (!decl.type) return compilerOptions.useUnknownInCatchVariables ? unknownType : anyType;
86078607
const type = getTypeOfNode(decl.type);
86088608
// an errorType will make `checkTryStatement` issue an error
86098609
return isTypeAny(type) || type === unknownType ? type : errorType;

src/compiler/commandLineParser.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,14 @@ namespace ts {
668668
category: Diagnostics.Additional_Checks,
669669
description: Diagnostics.Require_undeclared_properties_from_index_signatures_to_use_element_accesses
670670
},
671+
{
672+
name: "useUnknownInCatchVariables",
673+
type: "boolean",
674+
affectsSemanticDiagnostics: true,
675+
showInSimplifiedHelpView: false,
676+
category: Diagnostics.Additional_Checks,
677+
description: Diagnostics.Type_catch_clause_variables_as_unknown_instead_of_any,
678+
},
671679

672680
// Module Resolution
673681
{

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4757,6 +4757,10 @@
47574757
"category": "Message",
47584758
"code": 6800
47594759
},
4760+
"Type catch clause variables as 'unknown' instead of 'any'.": {
4761+
"category": "Message",
4762+
"code": 6801
4763+
},
47604764

47614765
"Variable '{0}' implicitly has an '{1}' type.": {
47624766
"category": "Error",

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5829,6 +5829,7 @@ namespace ts {
58295829
/* @internal */ suppressOutputPathCheck?: boolean;
58305830
target?: ScriptTarget; // TODO: GH#18217 frequently asserted as defined
58315831
traceResolution?: boolean;
5832+
useUnknownInCatchVariables?: boolean;
58325833
resolveJsonModule?: boolean;
58335834
types?: string[];
58345835
/** Paths used to compute primary types search locations */

0 commit comments

Comments
 (0)