Skip to content

Commit

Permalink
Help TypeScript propagate the Error.code type
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Feb 6, 2024
1 parent 63a2f96 commit 9d9d071
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,18 @@ function createErrorClass(name, {
: string;

const Error = class extends base {
static code = code;

code = code;

name = name;

/** @param {TData} [data] */
constructor(data) {
// @ts-expect-error TS2345 This is actually unsafe but the generic TData type
// is hard to express correctly in JSDoc.
const i18nKey = getString(data);
super(t(i18nKey, data ?? {}) ?? undefined);
this.name = name;
this.code = code;
this.i18nKey = i18nKey;
this.data = data;
// http-errors overwrites the prototype of the error, so it will
Expand All @@ -102,8 +106,6 @@ function createErrorClass(name, {
}
};

Error.code = code;

return Error;
}

Expand Down

0 comments on commit 9d9d071

Please sign in to comment.