Add new 'useUnknownInCatchVariables' flag.

This commit is contained in:
Daniel Rosenwasser 2020-10-08 18:06:51 -07:00
parent 67b4b9076d
commit 475e201abf
4 changed files with 14 additions and 1 deletions

View File

@ -8603,7 +8603,7 @@ namespace ts {
const declaration = symbol.valueDeclaration;
if (isCatchClauseVariableDeclarationOrBindingElement(declaration)) {
const decl = declaration as VariableDeclaration;
if (!decl.type) return anyType;
if (!decl.type) return compilerOptions.useUnknownInCatchVariables ? unknownType : anyType;
const type = getTypeOfNode(decl.type);
// an errorType will make `checkTryStatement` issue an error
return isTypeAny(type) || type === unknownType ? type : errorType;

View File

@ -668,6 +668,14 @@ namespace ts {
category: Diagnostics.Additional_Checks,
description: Diagnostics.Require_undeclared_properties_from_index_signatures_to_use_element_accesses
},
{
name: "useUnknownInCatchVariables",
type: "boolean",
affectsSemanticDiagnostics: true,
showInSimplifiedHelpView: false,
category: Diagnostics.Additional_Checks,
description: Diagnostics.Type_catch_clause_variables_as_unknown_instead_of_any,
},
// Module Resolution
{

View File

@ -4757,6 +4757,10 @@
"category": "Message",
"code": 6800
},
"Type catch clause variables as 'unknown' instead of 'any'.": {
"category": "Message",
"code": 6801
},
"Variable '{0}' implicitly has an '{1}' type.": {
"category": "Error",

View File

@ -5829,6 +5829,7 @@ namespace ts {
/* @internal */ suppressOutputPathCheck?: boolean;
target?: ScriptTarget; // TODO: GH#18217 frequently asserted as defined
traceResolution?: boolean;
useUnknownInCatchVariables?: boolean;
resolveJsonModule?: boolean;
types?: string[];
/** Paths used to compute primary types search locations */