TypeScript/tests/baselines/reference/useUnknownInCatchVariables01.symbols
Daniel Rosenwasser 9906092db2
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.
2021-06-03 13:12:56 -07:00

72 lines
2.3 KiB
Plaintext

=== 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))
}