mirror of
https://github.com/hanzoai/tsgo.git
synced 2026-10-30 19:37:15 +00:00
49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
//// [tests/cases/conformance/types/any/narrowExceptionVariableInCatchClause.ts] ////
|
|
|
|
//// [narrowExceptionVariableInCatchClause.ts]
|
|
declare function isFooError(x: any): x is { type: 'foo'; dontPanic(); };
|
|
|
|
function tryCatch() {
|
|
try {
|
|
// do stuff...
|
|
}
|
|
catch (err) { // err is implicitly 'any' and cannot be annotated
|
|
|
|
if (isFooError(err)) {
|
|
err.dontPanic(); // OK
|
|
err.doPanic(); // ERROR: Property 'doPanic' does not exist on type '{...}'
|
|
}
|
|
|
|
else if (err instanceof Error) {
|
|
err.message;
|
|
err.massage; // ERROR: Property 'massage' does not exist on type 'Error'
|
|
}
|
|
|
|
else {
|
|
throw err;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//// [narrowExceptionVariableInCatchClause.js]
|
|
"use strict";
|
|
function tryCatch() {
|
|
try {
|
|
// do stuff...
|
|
}
|
|
catch (err) { // err is implicitly 'any' and cannot be annotated
|
|
if (isFooError(err)) {
|
|
err.dontPanic(); // OK
|
|
err.doPanic(); // ERROR: Property 'doPanic' does not exist on type '{...}'
|
|
}
|
|
else if (err instanceof Error) {
|
|
err.message;
|
|
err.massage; // ERROR: Property 'massage' does not exist on type 'Error'
|
|
}
|
|
else {
|
|
throw err;
|
|
}
|
|
}
|
|
}
|