Skip to content

Add flag to change catch variables' default types to unknown #41013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 3, 2021
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ namespace ts {
const strictOptionalProperties = getStrictOptionValue(compilerOptions, "strictOptionalProperties");
const noImplicitAny = getStrictOptionValue(compilerOptions, "noImplicitAny");
const noImplicitThis = getStrictOptionValue(compilerOptions, "noImplicitThis");
const useUnknownInCatchVariables = getStrictOptionValue(compilerOptions, "useUnknownInCatchVariables");
const keyofStringsOnly = !!compilerOptions.keyofStringsOnly;
const freshObjectLiteralFlag = compilerOptions.suppressExcessPropertyErrors ? 0 : ObjectFlags.FreshLiteral;

Expand Down Expand Up @@ -9012,7 +9013,7 @@ namespace ts {
if (isCatchClauseVariableDeclarationOrBindingElement(declaration)) {
const typeNode = getEffectiveTypeAnnotationNode(declaration);
if (typeNode === undefined) {
return anyType;
return useUnknownInCatchVariables ? unknownType : anyType;
}
const type = getTypeOfNode(typeNode);
// an errorType will make `checkTryStatement` issue an error
Expand Down
9 changes: 9 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,15 @@ namespace ts {
category: Diagnostics.Type_Checking,
description: Diagnostics.Raise_error_on_this_expressions_with_an_implied_any_type,
},
{
name: "useUnknownInCatchVariables",
type: "boolean",
affectsSemanticDiagnostics: true,
strictFlag: true,
showInSimplifiedHelpView: true,
category: Diagnostics.Type_Checking,
description: Diagnostics.Type_catch_clause_variables_as_unknown_instead_of_any,
},
{
name: "alwaysStrict",
type: "boolean",
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -5198,6 +5198,10 @@
"category": "Message",
"code": 6802
},
"Type catch clause variables as 'unknown' instead of 'any'.": {
"category": "Message",
"code": 6803
},

"Variable '{0}' implicitly has an '{1}' type.": {
"category": "Error",
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6048,6 +6048,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 */
Expand Down
1 change: 1 addition & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6088,6 +6088,7 @@ namespace ts {
| "strictPropertyInitialization"
| "strictOptionalProperties"
| "alwaysStrict"
| "useUnknownInCatchVariables"
;

export function getStrictOptionValue(compilerOptions: CompilerOptions, flag: StrictOptionName): boolean {
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2919,6 +2919,7 @@ declare namespace ts {
suppressImplicitAnyIndexErrors?: boolean;
target?: ScriptTarget;
traceResolution?: boolean;
useUnknownInCatchVariables?: boolean;
resolveJsonModule?: boolean;
types?: string[];
/** Paths used to compute primary types search locations */
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2919,6 +2919,7 @@ declare namespace ts {
suppressImplicitAnyIndexErrors?: boolean;
target?: ScriptTarget;
traceResolution?: boolean;
useUnknownInCatchVariables?: boolean;
resolveJsonModule?: boolean;
types?: string[];
/** Paths used to compute primary types search locations */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class Foo {
>Aborter : typeof Aborter

} catch (error) {
>error : any
>error : unknown

if (this.abortController !== undefined) {
>this.abortController !== undefined : boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"useUnknownInCatchVariables": true
}
}
30 changes: 15 additions & 15 deletions tests/baselines/reference/tryCatchFinallyControlFlow.types
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ function f1() {
>a : number
}
catch (e) {
>e : any
>e : unknown

throw e;
>e : any
>e : unknown
}
finally {
if (a != null && a.toFixed(0) == "123") {
Expand Down Expand Up @@ -55,15 +55,15 @@ function f2() {
>1 : 1
}
catch (e) {
>e : any
>e : unknown

x = 2;
>x = 2 : 2
>x : 0 | 1 | 2 | 3
>2 : 2

throw e;
>e : any
>e : unknown
}
finally {
x; // 0 | 1 | 2
Expand All @@ -87,7 +87,7 @@ function f3() {
>1 : 1
}
catch (e) {
>e : any
>e : unknown

x = 2;
>x = 2 : 2
Expand Down Expand Up @@ -118,7 +118,7 @@ function f4() {
>1 : 1
}
catch (e) {
>e : any
>e : unknown

x = 2;
>x = 2 : 2
Expand Down Expand Up @@ -149,7 +149,7 @@ function f5() {
return;
}
catch (e) {
>e : any
>e : unknown

x = 2;
>x = 2 : 2
Expand Down Expand Up @@ -178,7 +178,7 @@ function f6() {
>1 : 1
}
catch (e) {
>e : any
>e : unknown

x = 2;
>x = 2 : 2
Expand Down Expand Up @@ -211,7 +211,7 @@ function f7() {
return;
}
catch (e) {
>e : any
>e : unknown

x = 2;
>x = 2 : 2
Expand Down Expand Up @@ -324,7 +324,7 @@ function f10() {
return;
}
catch (e) {
>e : any
>e : unknown

x = 2;
>x = 2 : 2
Expand Down Expand Up @@ -388,7 +388,7 @@ function f11() {
}
}
catch (e) {
>e : any
>e : unknown

x; // 0 | 1 | 2
>x : 0 | 1 | 2
Expand Down Expand Up @@ -466,7 +466,7 @@ function f12() {
}
}
catch (e) {
>e : any
>e : unknown

x; // 0 | 1 | 2
>x : 0 | 1 | 2
Expand Down Expand Up @@ -576,7 +576,7 @@ function t1() {
>'x' : "x"
}
catch (e) {
>e : any
>e : unknown

return null;
>null : null
Expand Down Expand Up @@ -626,7 +626,7 @@ function notallowed(arg: number) {
finally { }
}
catch (err) {
>err : any
>err : unknown

state.tag;
>state.tag : "one" | "two" | "three"
Expand Down Expand Up @@ -770,7 +770,7 @@ function f21() {
>x : 3 | 4 | 5
}
catch (e) {
>e : any
>e : unknown

x; // 0 | 1 | 2 | 3 | 4 | 5
>x : 0 | 1 | 2 | 3 | 4 | 5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
"noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Options:
--strictPropertyInitialization Enable strict checking of property initialization in classes.
--strictOptionalProperties Enable strict checking of optional properties.
--noImplicitThis Raise error on 'this' expressions with an implied 'any' type.
--useUnknownInCatchVariables Type catch clause variables as 'unknown' instead of 'any'.
--alwaysStrict Parse in strict mode and emit "use strict" for each source file.
--noUnusedLocals Report errors on unused locals.
--noUnusedParameters Report errors on unused parameters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ interface Array<T> { length: number; [n: number]: T; }
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ interface Array<T> { length: number; [n: number]: T; }
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ interface Array<T> { length: number; [n: number]: T; }
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ interface Array<T> { length: number; [n: number]: T; }
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ interface Array<T> { length: number; [n: number]: T; }
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ interface Array<T> { length: number; [n: number]: T; }
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/typedefOnStatements.types
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ throw new Error('Unreachable')
try {
}
catch (e) {
>e : any
>e : unknown
}

/**
Expand Down
Loading