Skip to content

Commit 87b36c9

Browse files
Accepted baselines.
1 parent 89a44a0 commit 87b36c9

File tree

5 files changed

+200
-0
lines changed

5 files changed

+200
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"useUnknownInCatchVariables": true
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
tests/cases/compiler/useUnknownInCatchVariables01.ts(6,12): error TS2339: Property 'toUpperCase' does not exist on type 'unknown'.
2+
tests/cases/compiler/useUnknownInCatchVariables01.ts(7,10): error TS2356: An arithmetic operand must be of type 'any', 'number', 'bigint' or an enum type.
3+
tests/cases/compiler/useUnknownInCatchVariables01.ts(8,10): error TS2349: This expression is not callable.
4+
Type '{}' has no call signatures.
5+
6+
7+
==== tests/cases/compiler/useUnknownInCatchVariables01.ts (3 errors) ====
8+
try {
9+
// ...
10+
}
11+
catch (e) {
12+
// error!
13+
void e.toUpperCase();
14+
~~~~~~~~~~~
15+
!!! error TS2339: Property 'toUpperCase' does not exist on type 'unknown'.
16+
void e++;
17+
~
18+
!!! error TS2356: An arithmetic operand must be of type 'any', 'number', 'bigint' or an enum type.
19+
void e();
20+
~
21+
!!! error TS2349: This expression is not callable.
22+
!!! error TS2349: Type '{}' has no call signatures.
23+
24+
if (typeof e === "string") {
25+
// works!
26+
// We've narrowed 'e' down to the type 'string'.
27+
console.log(e.toUpperCase());
28+
}
29+
if (e instanceof Error) {
30+
e.stack?.toUpperCase();
31+
}
32+
if (typeof e === "number") {
33+
e++;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//// [useUnknownInCatchVariables01.ts]
2+
try {
3+
// ...
4+
}
5+
catch (e) {
6+
// error!
7+
void e.toUpperCase();
8+
void e++;
9+
void e();
10+
11+
if (typeof e === "string") {
12+
// works!
13+
// We've narrowed 'e' down to the type 'string'.
14+
console.log(e.toUpperCase());
15+
}
16+
if (e instanceof Error) {
17+
e.stack?.toUpperCase();
18+
}
19+
if (typeof e === "number") {
20+
e++;
21+
}
22+
}
23+
24+
//// [useUnknownInCatchVariables01.js]
25+
var _a;
26+
try {
27+
// ...
28+
}
29+
catch (e) {
30+
// error!
31+
void e.toUpperCase();
32+
void e++;
33+
void e();
34+
if (typeof e === "string") {
35+
// works!
36+
// We've narrowed 'e' down to the type 'string'.
37+
console.log(e.toUpperCase());
38+
}
39+
if (e instanceof Error) {
40+
(_a = e.stack) === null || _a === void 0 ? void 0 : _a.toUpperCase();
41+
}
42+
if (typeof e === "number") {
43+
e++;
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
=== tests/cases/compiler/useUnknownInCatchVariables01.ts ===
2+
try {
3+
// ...
4+
}
5+
catch (e) {
6+
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 3, 7))
7+
8+
// error!
9+
void e.toUpperCase();
10+
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 3, 7))
11+
12+
void e++;
13+
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 3, 7))
14+
15+
void e();
16+
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 3, 7))
17+
18+
if (typeof e === "string") {
19+
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 3, 7))
20+
21+
// works!
22+
// We've narrowed 'e' down to the type 'string'.
23+
console.log(e.toUpperCase());
24+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
25+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
26+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
27+
>e.toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --))
28+
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 3, 7))
29+
>toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --))
30+
}
31+
if (e instanceof Error) {
32+
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 3, 7))
33+
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
34+
35+
e.stack?.toUpperCase();
36+
>e.stack?.toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --))
37+
>e.stack : Symbol(Error.stack, Decl(lib.es5.d.ts, --, --))
38+
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 3, 7))
39+
>stack : Symbol(Error.stack, Decl(lib.es5.d.ts, --, --))
40+
>toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --))
41+
}
42+
if (typeof e === "number") {
43+
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 3, 7))
44+
45+
e++;
46+
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 3, 7))
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
=== tests/cases/compiler/useUnknownInCatchVariables01.ts ===
2+
try {
3+
// ...
4+
}
5+
catch (e) {
6+
>e : unknown
7+
8+
// error!
9+
void e.toUpperCase();
10+
>void e.toUpperCase() : undefined
11+
>e.toUpperCase() : any
12+
>e.toUpperCase : any
13+
>e : unknown
14+
>toUpperCase : any
15+
16+
void e++;
17+
>void e++ : undefined
18+
>e++ : number
19+
>e : unknown
20+
21+
void e();
22+
>void e() : undefined
23+
>e() : any
24+
>e : unknown
25+
26+
if (typeof e === "string") {
27+
>typeof e === "string" : boolean
28+
>typeof e : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
29+
>e : unknown
30+
>"string" : "string"
31+
32+
// works!
33+
// We've narrowed 'e' down to the type 'string'.
34+
console.log(e.toUpperCase());
35+
>console.log(e.toUpperCase()) : void
36+
>console.log : (...data: any[]) => void
37+
>console : Console
38+
>log : (...data: any[]) => void
39+
>e.toUpperCase() : string
40+
>e.toUpperCase : () => string
41+
>e : string
42+
>toUpperCase : () => string
43+
}
44+
if (e instanceof Error) {
45+
>e instanceof Error : boolean
46+
>e : unknown
47+
>Error : ErrorConstructor
48+
49+
e.stack?.toUpperCase();
50+
>e.stack?.toUpperCase() : string
51+
>e.stack?.toUpperCase : () => string
52+
>e.stack : string
53+
>e : Error
54+
>stack : string
55+
>toUpperCase : () => string
56+
}
57+
if (typeof e === "number") {
58+
>typeof e === "number" : boolean
59+
>typeof e : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
60+
>e : unknown
61+
>"number" : "number"
62+
63+
e++;
64+
>e++ : number
65+
>e : number
66+
}
67+
}

0 commit comments

Comments
 (0)