I currently have Typescript 4.0.2 installed. Within lib.es5.d.ts, there is the snippet provided below:
interface Error {
name: string;
message: string;
stack?: string;
}
interface ErrorConstructor {
new(message?: string): Error;
(message?: string): Error;
readonly prototype: Error;
}
declare var Error: ErrorConstructor;
I am successfully using
const err = new Error("some message");
My goal is to enhance the Error
interface by adding a property named statusCode
specifically for handling Http errors. I attempted the following:
interface HttpException extends Error {
// new(message?: string): Error;
// (message?: string): HttpException;
// readonly prototype: HttpException;
statusCode?: number
}
However, I encountered an issue when trying to use
const e = new HttpException("Not found");
. The error message stated 'HttpError' only refers to a type, but is being used as a value here. (ts2693)
Why can't HttpException
be utilized in the same way as Error
?