Skip to content

Commit 9906092

Browse files
Add flag to change catch variables' default types to unknown (#41013)
* Add test case for 'useUnknownInCatchVariables'. * Add new 'useUnknownInCatchVariables' flag. * Accepted baselines. * Add test for catch variable explicitly typed as 'any'. * Accepted baselines. * Move option under 'strict'. * Accepted baselines. * 'useUnknownInCatchVariables' is strict in command line help.
1 parent 6baa1be commit 9906092

File tree

32 files changed

+378
-18
lines changed

32 files changed

+378
-18
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ namespace ts {
346346
const strictOptionalProperties = getStrictOptionValue(compilerOptions, "strictOptionalProperties");
347347
const noImplicitAny = getStrictOptionValue(compilerOptions, "noImplicitAny");
348348
const noImplicitThis = getStrictOptionValue(compilerOptions, "noImplicitThis");
349+
const useUnknownInCatchVariables = getStrictOptionValue(compilerOptions, "useUnknownInCatchVariables");
349350
const keyofStringsOnly = !!compilerOptions.keyofStringsOnly;
350351
const freshObjectLiteralFlag = compilerOptions.suppressExcessPropertyErrors ? 0 : ObjectFlags.FreshLiteral;
351352

@@ -9012,7 +9013,7 @@ namespace ts {
90129013
if (isCatchClauseVariableDeclarationOrBindingElement(declaration)) {
90139014
const typeNode = getEffectiveTypeAnnotationNode(declaration);
90149015
if (typeNode === undefined) {
9015-
return anyType;
9016+
return useUnknownInCatchVariables ? unknownType : anyType;
90169017
}
90179018
const type = getTypeOfNode(typeNode);
90189019
// an errorType will make `checkTryStatement` issue an error

src/compiler/commandLineParser.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,15 @@ namespace ts {
632632
category: Diagnostics.Type_Checking,
633633
description: Diagnostics.Raise_error_on_this_expressions_with_an_implied_any_type,
634634
},
635+
{
636+
name: "useUnknownInCatchVariables",
637+
type: "boolean",
638+
affectsSemanticDiagnostics: true,
639+
strictFlag: true,
640+
showInSimplifiedHelpView: true,
641+
category: Diagnostics.Type_Checking,
642+
description: Diagnostics.Type_catch_clause_variables_as_unknown_instead_of_any,
643+
},
635644
{
636645
name: "alwaysStrict",
637646
type: "boolean",

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5198,6 +5198,10 @@
51985198
"category": "Message",
51995199
"code": 6802
52005200
},
5201+
"Type catch clause variables as 'unknown' instead of 'any'.": {
5202+
"category": "Message",
5203+
"code": 6803
5204+
},
52015205

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

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6048,6 +6048,7 @@ namespace ts {
60486048
/* @internal */ suppressOutputPathCheck?: boolean;
60496049
target?: ScriptTarget; // TODO: GH#18217 frequently asserted as defined
60506050
traceResolution?: boolean;
6051+
useUnknownInCatchVariables?: boolean;
60516052
resolveJsonModule?: boolean;
60526053
types?: string[];
60536054
/** Paths used to compute primary types search locations */

src/compiler/utilities.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6088,6 +6088,7 @@ namespace ts {
60886088
| "strictPropertyInitialization"
60896089
| "strictOptionalProperties"
60906090
| "alwaysStrict"
6091+
| "useUnknownInCatchVariables"
60916092
;
60926093

60936094
export function getStrictOptionValue(compilerOptions: CompilerOptions, flag: StrictOptionName): boolean {

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2919,6 +2919,7 @@ declare namespace ts {
29192919
suppressImplicitAnyIndexErrors?: boolean;
29202920
target?: ScriptTarget;
29212921
traceResolution?: boolean;
2922+
useUnknownInCatchVariables?: boolean;
29222923
resolveJsonModule?: boolean;
29232924
types?: string[];
29242925
/** Paths used to compute primary types search locations */

tests/baselines/reference/api/typescript.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2919,6 +2919,7 @@ declare namespace ts {
29192919
suppressImplicitAnyIndexErrors?: boolean;
29202920
target?: ScriptTarget;
29212921
traceResolution?: boolean;
2922+
useUnknownInCatchVariables?: boolean;
29222923
resolveJsonModule?: boolean;
29232924
types?: string[];
29242925
/** Paths used to compute primary types search locations */

tests/baselines/reference/controlFlowForCatchAndFinally.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class Foo {
120120
>Aborter : typeof Aborter
121121

122122
} catch (error) {
123-
>error : any
123+
>error : unknown
124124

125125
if (this.abortController !== undefined) {
126126
>this.abortController !== undefined : boolean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"useUnknownInCatchVariables": true
4+
}
5+
}

tests/baselines/reference/tryCatchFinallyControlFlow.types

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ function f1() {
1919
>a : number
2020
}
2121
catch (e) {
22-
>e : any
22+
>e : unknown
2323

2424
throw e;
25-
>e : any
25+
>e : unknown
2626
}
2727
finally {
2828
if (a != null && a.toFixed(0) == "123") {
@@ -55,15 +55,15 @@ function f2() {
5555
>1 : 1
5656
}
5757
catch (e) {
58-
>e : any
58+
>e : unknown
5959

6060
x = 2;
6161
>x = 2 : 2
6262
>x : 0 | 1 | 2 | 3
6363
>2 : 2
6464

6565
throw e;
66-
>e : any
66+
>e : unknown
6767
}
6868
finally {
6969
x; // 0 | 1 | 2
@@ -87,7 +87,7 @@ function f3() {
8787
>1 : 1
8888
}
8989
catch (e) {
90-
>e : any
90+
>e : unknown
9191

9292
x = 2;
9393
>x = 2 : 2
@@ -118,7 +118,7 @@ function f4() {
118118
>1 : 1
119119
}
120120
catch (e) {
121-
>e : any
121+
>e : unknown
122122

123123
x = 2;
124124
>x = 2 : 2
@@ -149,7 +149,7 @@ function f5() {
149149
return;
150150
}
151151
catch (e) {
152-
>e : any
152+
>e : unknown
153153

154154
x = 2;
155155
>x = 2 : 2
@@ -178,7 +178,7 @@ function f6() {
178178
>1 : 1
179179
}
180180
catch (e) {
181-
>e : any
181+
>e : unknown
182182

183183
x = 2;
184184
>x = 2 : 2
@@ -211,7 +211,7 @@ function f7() {
211211
return;
212212
}
213213
catch (e) {
214-
>e : any
214+
>e : unknown
215215

216216
x = 2;
217217
>x = 2 : 2
@@ -324,7 +324,7 @@ function f10() {
324324
return;
325325
}
326326
catch (e) {
327-
>e : any
327+
>e : unknown
328328

329329
x = 2;
330330
>x = 2 : 2
@@ -388,7 +388,7 @@ function f11() {
388388
}
389389
}
390390
catch (e) {
391-
>e : any
391+
>e : unknown
392392

393393
x; // 0 | 1 | 2
394394
>x : 0 | 1 | 2
@@ -466,7 +466,7 @@ function f12() {
466466
}
467467
}
468468
catch (e) {
469-
>e : any
469+
>e : unknown
470470

471471
x; // 0 | 1 | 2
472472
>x : 0 | 1 | 2
@@ -576,7 +576,7 @@ function t1() {
576576
>'x' : "x"
577577
}
578578
catch (e) {
579-
>e : any
579+
>e : unknown
580580

581581
return null;
582582
>null : null
@@ -626,7 +626,7 @@ function notallowed(arg: number) {
626626
finally { }
627627
}
628628
catch (err) {
629-
>err : any
629+
>err : unknown
630630

631631
state.tag;
632632
>state.tag : "one" | "two" | "three"
@@ -770,7 +770,7 @@ function f21() {
770770
>x : 3 | 4 | 5
771771
}
772772
catch (e) {
773-
>e : any
773+
>e : unknown
774774

775775
x; // 0 | 1 | 2 | 3 | 4 | 5
776776
>x : 0 | 1 | 2 | 3 | 4 | 5

tests/baselines/reference/tsConfig/Default initialized TSConfig/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
8282
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
8383
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
84+
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
8485
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
8586
// "noUnusedLocals": true, /* Report errors on unused locals. */
8687
// "noUnusedParameters": true, /* Report errors on unused parameters. */

tests/baselines/reference/tsConfig/Initialized TSConfig with advanced options/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
8282
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
8383
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
84+
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
8485
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
8586
// "noUnusedLocals": true, /* Report errors on unused locals. */
8687
// "noUnusedParameters": true, /* Report errors on unused parameters. */

tests/baselines/reference/tsConfig/Initialized TSConfig with boolean value compiler options/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
8282
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
8383
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
84+
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
8485
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
8586
"noUnusedLocals": true, /* Report errors on unused locals. */
8687
// "noUnusedParameters": true, /* Report errors on unused parameters. */

tests/baselines/reference/tsConfig/Initialized TSConfig with enum value compiler options/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
8282
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
8383
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
84+
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
8485
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
8586
// "noUnusedLocals": true, /* Report errors on unused locals. */
8687
// "noUnusedParameters": true, /* Report errors on unused parameters. */

tests/baselines/reference/tsConfig/Initialized TSConfig with files options/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
8282
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
8383
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
84+
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
8485
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
8586
// "noUnusedLocals": true, /* Report errors on unused locals. */
8687
// "noUnusedParameters": true, /* Report errors on unused parameters. */

tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option value/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
8282
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
8383
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
84+
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
8485
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
8586
// "noUnusedLocals": true, /* Report errors on unused locals. */
8687
// "noUnusedParameters": true, /* Report errors on unused parameters. */

tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
8282
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
8383
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
84+
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
8485
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
8586
// "noUnusedLocals": true, /* Report errors on unused locals. */
8687
// "noUnusedParameters": true, /* Report errors on unused parameters. */

tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options with enum value/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
8282
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
8383
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
84+
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
8485
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
8586
// "noUnusedLocals": true, /* Report errors on unused locals. */
8687
// "noUnusedParameters": true, /* Report errors on unused parameters. */

tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
8282
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
8383
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
84+
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
8485
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
8586
// "noUnusedLocals": true, /* Report errors on unused locals. */
8687
// "noUnusedParameters": true, /* Report errors on unused parameters. */

tests/baselines/reference/tsc/runWithoutArgs/initial-build/show-help-with-ExitStatus.DiagnosticsPresent_OutputsSkipped.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Options:
4444
--strictPropertyInitialization Enable strict checking of property initialization in classes.
4545
--strictOptionalProperties Enable strict checking of optional properties.
4646
--noImplicitThis Raise error on 'this' expressions with an implied 'any' type.
47+
--useUnknownInCatchVariables Type catch clause variables as 'unknown' instead of 'any'.
4748
--alwaysStrict Parse in strict mode and emit "use strict" for each source file.
4849
--noUnusedLocals Report errors on unused locals.
4950
--noUnusedParameters Report errors on unused parameters.

tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/declarationDir-is-specified.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ interface Array<T> { length: number; [n: number]: T; }
102102
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
103103
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
104104
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
105+
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
105106
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
106107
// "noUnusedLocals": true, /* Report errors on unused locals. */
107108
// "noUnusedParameters": true, /* Report errors on unused parameters. */

tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-and-declarationDir-is-specified.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ interface Array<T> { length: number; [n: number]: T; }
102102
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
103103
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
104104
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
105+
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
105106
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
106107
// "noUnusedLocals": true, /* Report errors on unused locals. */
107108
// "noUnusedParameters": true, /* Report errors on unused parameters. */

tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-is-specified.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ interface Array<T> { length: number; [n: number]: T; }
102102
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
103103
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
104104
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
105+
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
105106
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
106107
// "noUnusedLocals": true, /* Report errors on unused locals. */
107108
// "noUnusedParameters": true, /* Report errors on unused parameters. */

tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/with-outFile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ interface Array<T> { length: number; [n: number]: T; }
102102
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
103103
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
104104
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
105+
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
105106
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
106107
// "noUnusedLocals": true, /* Report errors on unused locals. */
107108
// "noUnusedParameters": true, /* Report errors on unused parameters. */

tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified-with-declaration-enabled.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ interface Array<T> { length: number; [n: number]: T; }
102102
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
103103
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
104104
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
105+
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
105106
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
106107
// "noUnusedLocals": true, /* Report errors on unused locals. */
107108
// "noUnusedParameters": true, /* Report errors on unused parameters. */

tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ interface Array<T> { length: number; [n: number]: T; }
102102
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
103103
// "strictOptionalProperties": true, /* Enable strict checking of optional properties. */
104104
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
105+
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
105106
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
106107
// "noUnusedLocals": true, /* Report errors on unused locals. */
107108
// "noUnusedParameters": true, /* Report errors on unused parameters. */

tests/baselines/reference/typedefOnStatements.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ throw new Error('Unreachable')
6868
try {
6969
}
7070
catch (e) {
71-
>e : any
71+
>e : unknown
7272
}
7373

7474
/**

0 commit comments

Comments
 (0)