Skip to content

types: add generics to isError and update DataT default generic param #582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { hasProp } from "./utils/internal/object";
* This can be used to pass additional information about the error.
* @property {boolean} internal - Setting this property to `true` will mark the error as an internal error.
*/
export class H3Error<DataT = any> extends Error {
export class H3Error<DataT = unknown> extends Error {
static __h3_error__ = true;
statusCode = 500;
fatal = false;
Expand Down Expand Up @@ -64,16 +64,16 @@ export class H3Error<DataT = any> extends Error {
* @param input {string | (Partial<H3Error> & { status?: number; statusText?: string })} - The error message or an object containing error properties.
* @return {H3Error} - An instance of H3Error.
*/
export function createError<DataT = any>(
export function createError<DataT = unknown>(
input:
| string
| (Partial<H3Error<DataT>> & { status?: number; statusText?: string }),
): H3Error {
) {
if (typeof input === "string") {
return new H3Error<DataT>(input);
}

if (isError(input)) {
if (isError<DataT>(input)) {
return input;
}

Expand Down Expand Up @@ -177,6 +177,6 @@ export function sendError(
* @param input {*} - The input to check.
* @return {boolean} - Returns true if the input is an instance of H3Error, false otherwise.
*/
export function isError(input: any): input is H3Error {
export function isError<DataT = unknown>(input: any): input is H3Error<DataT> {
return input?.constructor?.__h3_error__ === true;
}