1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
=== 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.toExponential();
>e.toExponential : Symbol(Number.toExponential, Decl(lib.es5.d.ts, --, --))
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 3, 7))
>toExponential : Symbol(Number.toExponential, Decl(lib.es5.d.ts, --, --))
e++;
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 3, 7))
}
}
try {
// ...
}
catch (e: any) {
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 27, 7))
// All are allowed.
void e.toUpperCase();
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 27, 7))
void e.toExponential();
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 27, 7))
void e();
>e : Symbol(e, Decl(useUnknownInCatchVariables01.ts, 27, 7))
}
|