Skip to content

Commit 89a44a0

Browse files
Add new 'useUnknownInCatchVariables' flag.
1 parent 757d6ca commit 89a44a0

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8556,7 +8556,7 @@ namespace ts {
85568556
const declaration = symbol.valueDeclaration;
85578557
if (isCatchClauseVariableDeclarationOrBindingElement(declaration)) {
85588558
const decl = declaration as VariableDeclaration;
8559-
if (!decl.type) return anyType;
8559+
if (!decl.type) return compilerOptions.useUnknownInCatchVariables ? unknownType : anyType;
85608560
const type = getTypeOfNode(decl.type);
85618561
// an errorType will make `checkTryStatement` issue an error
85628562
return isTypeAny(type) || type === unknownType ? type : errorType;

src/compiler/commandLineParser.ts

+8
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,14 @@ namespace ts {
634634
category: Diagnostics.Additional_Checks,
635635
description: Diagnostics.Include_undefined_in_index_signature_results
636636
},
637+
{
638+
name: "useUnknownInCatchVariables",
639+
type: "boolean",
640+
affectsSemanticDiagnostics: true,
641+
showInSimplifiedHelpView: false,
642+
category: Diagnostics.Additional_Checks,
643+
description: Diagnostics.Type_catch_clause_variables_as_unknown_instead_of_any,
644+
},
637645

638646
// Module Resolution
639647
{

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -4742,6 +4742,10 @@
47424742
"category": "Message",
47434743
"code": 6800
47444744
},
4745+
"Type catch clause variables as 'unknown' instead of 'any'.": {
4746+
"category": "Message",
4747+
"code": 6801
4748+
},
47454749

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

src/compiler/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5819,6 +5819,7 @@ namespace ts {
58195819
/* @internal */ suppressOutputPathCheck?: boolean;
58205820
target?: ScriptTarget; // TODO: GH#18217 frequently asserted as defined
58215821
traceResolution?: boolean;
5822+
useUnknownInCatchVariables?: boolean;
58225823
resolveJsonModule?: boolean;
58235824
types?: string[];
58245825
/** Paths used to compute primary types search locations */

0 commit comments

Comments
 (0)