-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
89a44a0
commit 87b36c9
Showing
5 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
...ence/showConfig/Shows tsconfig for single option/useUnknownInCatchVariables/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"compilerOptions": { | ||
"useUnknownInCatchVariables": true | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
tests/baselines/reference/useUnknownInCatchVariables01.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
tests/cases/compiler/useUnknownInCatchVariables01.ts(6,12): error TS2339: Property 'toUpperCase' does not exist on type 'unknown'. | ||
tests/cases/compiler/useUnknownInCatchVariables01.ts(7,10): error TS2356: An arithmetic operand must be of type 'any', 'number', 'bigint' or an enum type. | ||
tests/cases/compiler/useUnknownInCatchVariables01.ts(8,10): error TS2349: This expression is not callable. | ||
Type '{}' has no call signatures. | ||
|
||
|
||
==== tests/cases/compiler/useUnknownInCatchVariables01.ts (3 errors) ==== | ||
try { | ||
// ... | ||
} | ||
catch (e) { | ||
// error! | ||
void e.toUpperCase(); | ||
~~~~~~~~~~~ | ||
!!! error TS2339: Property 'toUpperCase' does not exist on type 'unknown'. | ||
void e++; | ||
~ | ||
!!! error TS2356: An arithmetic operand must be of type 'any', 'number', 'bigint' or an enum type. | ||
void e(); | ||
~ | ||
!!! error TS2349: This expression is not callable. | ||
!!! error TS2349: Type '{}' has no call signatures. | ||
|
||
if (typeof e === "string") { | ||
// works! | ||
// We've narrowed 'e' down to the type 'string'. | ||
console.log(e.toUpperCase()); | ||
} | ||
if (e instanceof Error) { | ||
e.stack?.toUpperCase(); | ||
} | ||
if (typeof e === "number") { | ||
e++; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//// [useUnknownInCatchVariables01.ts] | ||
try { | ||
// ... | ||
} | ||
catch (e) { | ||
// error! | ||
void e.toUpperCase(); | ||
void e++; | ||
void e(); | ||
|
||
if (typeof e === "string") { | ||
// works! | ||
// We've narrowed 'e' down to the type 'string'. | ||
console.log(e.toUpperCase()); | ||
} | ||
if (e instanceof Error) { | ||
e.stack?.toUpperCase(); | ||
} | ||
if (typeof e === "number") { | ||
e++; | ||
} | ||
} | ||
|
||
//// [useUnknownInCatchVariables01.js] | ||
var _a; | ||
try { | ||
// ... | ||
} | ||
catch (e) { | ||
// error! | ||
void e.toUpperCase(); | ||
void e++; | ||
void e(); | ||
if (typeof e === "string") { | ||
// works! | ||
// We've narrowed 'e' down to the type 'string'. | ||
console.log(e.toUpperCase()); | ||
} | ||
if (e instanceof Error) { | ||
(_a = e.stack) === null || _a === void 0 ? void 0 : _a.toUpperCase(); | ||
} | ||
if (typeof e === "number") { | ||
e++; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
tests/baselines/reference/useUnknownInCatchVariables01.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
=== tests/cases/compiler/useUnknownInCatchVariables01.ts === | ||
try { | ||
// ... | ||
} | ||
catch (e) { | ||
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 3, 7)) | ||
|
||
// error! | ||
void e.toUpperCase(); | ||
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 3, 7)) | ||
|
||
void e++; | ||
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 3, 7)) | ||
|
||
void e(); | ||
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 3, 7)) | ||
|
||
if (typeof e === "string") { | ||
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 3, 7)) | ||
|
||
// works! | ||
// We've narrowed 'e' down to the type 'string'. | ||
console.log(e.toUpperCase()); | ||
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>e.toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) | ||
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 3, 7)) | ||
>toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) | ||
} | ||
if (e instanceof Error) { | ||
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 3, 7)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
|
||
e.stack?.toUpperCase(); | ||
>e.stack?.toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) | ||
>e.stack : Symbol(Error.stack, Decl(lib.es5.d.ts, --, --)) | ||
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 3, 7)) | ||
>stack : Symbol(Error.stack, Decl(lib.es5.d.ts, --, --)) | ||
>toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) | ||
} | ||
if (typeof e === "number") { | ||
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 3, 7)) | ||
|
||
e++; | ||
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 3, 7)) | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
tests/baselines/reference/useUnknownInCatchVariables01.types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
=== tests/cases/compiler/useUnknownInCatchVariables01.ts === | ||
try { | ||
// ... | ||
} | ||
catch (e) { | ||
>e : unknown | ||
|
||
// error! | ||
void e.toUpperCase(); | ||
>void e.toUpperCase() : undefined | ||
>e.toUpperCase() : any | ||
>e.toUpperCase : any | ||
>e : unknown | ||
>toUpperCase : any | ||
|
||
void e++; | ||
>void e++ : undefined | ||
>e++ : number | ||
>e : unknown | ||
|
||
void e(); | ||
>void e() : undefined | ||
>e() : any | ||
>e : unknown | ||
|
||
if (typeof e === "string") { | ||
>typeof e === "string" : boolean | ||
>typeof e : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | ||
>e : unknown | ||
>"string" : "string" | ||
|
||
// works! | ||
// We've narrowed 'e' down to the type 'string'. | ||
console.log(e.toUpperCase()); | ||
>console.log(e.toUpperCase()) : void | ||
>console.log : (...data: any[]) => void | ||
>console : Console | ||
>log : (...data: any[]) => void | ||
>e.toUpperCase() : string | ||
>e.toUpperCase : () => string | ||
>e : string | ||
>toUpperCase : () => string | ||
} | ||
if (e instanceof Error) { | ||
>e instanceof Error : boolean | ||
>e : unknown | ||
>Error : ErrorConstructor | ||
|
||
e.stack?.toUpperCase(); | ||
>e.stack?.toUpperCase() : string | ||
>e.stack?.toUpperCase : () => string | ||
>e.stack : string | ||
>e : Error | ||
>stack : string | ||
>toUpperCase : () => string | ||
} | ||
if (typeof e === "number") { | ||
>typeof e === "number" : boolean | ||
>typeof e : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | ||
>e : unknown | ||
>"number" : "number" | ||
|
||
e++; | ||
>e++ : number | ||
>e : number | ||
} | ||
} |