in the following code snippet, why is the behavior of the isTypeError type guard different, depending on whether the block is wrapped in an instanceof Error check or not - on line 6 error is narrowed to TypeError, and consequently on line 12, error is unknown - but on line 21, error is narrowed to Error and consequently error on line 27 is never (i had expected it to still be Error)
let error: unknown
declare function isTypeError(value: unknown): value is TypeError
if (isTypeError(error)) {
error
// ^?
throw new Error()
}
error
// ^?
if (error instanceof Error) {
error
// ^?
if (isTypeError(error)) {
// Why is this Error and not TypeError?
error
// ^?
throw new Error()
}
error
// ^?
}
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.