TypeScript/tests/cases/conformance/statements/tryStatements/catchClauseWithTypeAnnotation.ts
Eli Barzilay ffa35d3272 Allow e: unknown in catch arguments
In addition, allow an explicit `any`; anything else throws an error.

Also adjust and reorganize existing tests.

Fixes #36775.
2020-06-10 18:24:20 -04:00

34 lines
1.3 KiB
TypeScript

type any1 = any;
type unknown1 = unknown;
function fn(x: boolean) {
// no type annotation allowed other than `any` and `unknown`
try { } catch (x) { } // should be OK
try { } catch (x: any) { } // should be OK
try { } catch (x: any1) { } // should be OK
try { } catch (x: unknown) { } // should be OK
try { } catch (x: unknown1) { } // should be OK
try { } catch (x) { x.foo; } // should be OK
try { } catch (x: any) { x.foo; } // should be OK
try { } catch (x: any1) { x.foo; } // should be OK
try { } catch (x: unknown) { console.log(x); } // should be OK
try { } catch (x: unknown1) { console.log(x); } // should be OK
try { } catch (x: unknown) { x.foo; } // error in the body
try { } catch (x: unknown1) { x.foo; } // error in the body
try { } catch (x: Error) { } // error in the type
try { } catch (x: object) { } // error in the type
try { console.log(); }
// @ts-ignore
catch (e: number) { // e should not be a `number`
console.log(e.toLowerCase());
}
// minor bug: shows that the `catch` argument is skipped when checking scope
try { } catch (x) { let x: string; }
try { } catch (x) { var x: string; }
try { } catch (x) { var x: boolean; }
}