Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ namespace ts {
const strictPropertyInitialization = getStrictOptionValue(compilerOptions, "strictPropertyInitialization");
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 @@ -8929,7 +8930,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
8 changes: 8 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,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: true,
category: Diagnostics.Additional_Checks,
description: Diagnostics.Type_catch_clause_variables_as_unknown_instead_of_any,
},

// Module Resolution
{
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -5095,6 +5095,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 @@ -6006,6 +6006,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 @@ -6067,6 +6067,7 @@ namespace ts {
| "strictBindCallApply"
| "strictPropertyInitialization"
| "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 @@ -2896,6 +2896,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 @@ -2896,6 +2896,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 @@ -42,6 +42,7 @@
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Options:
--noUnusedParameters Report errors on unused parameters.
--noImplicitReturns Report error when not all code paths in function return a value.
--noFallthroughCasesInSwitch Report errors for fallthrough cases in switch statement.
--useUnknownInCatchVariables Type catch clause variables as 'unknown' instead of 'any'.
--types Type declaration files to be included in compilation.
--esModuleInterop Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'.
@<file> Insert command line options and files from a file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ interface Array<T> { length: number; [n: number]: T; }
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ interface Array<T> { length: number; [n: number]: T; }
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ interface Array<T> { length: number; [n: number]: T; }
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ interface Array<T> { length: number; [n: number]: T; }
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ interface Array<T> { length: number; [n: number]: T; }
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ interface Array<T> { length: number; [n: number]: T; }
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
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