Skip to content
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

Strictly type createError #89

Merged
merged 4 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 8 additions & 6 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
type CreateError = (code: string, message: string, statusCode?: number, Base?: Error) => createError.FastifyErrorConstructor
declare function createError<C extends string, SC extends number>(code: C, message: string, statusCode: SC, Base?: Error): createError.FastifyErrorConstructor<{ code: C; statusCode: SC }>;
declare function createError<C extends string>(code: C, message: string, statusCode?: number, Base?: Error): createError.FastifyErrorConstructor<{ code: C; }>;

type CreateError = typeof createError;

declare namespace createError {
export interface FastifyError extends Error {
Expand All @@ -7,15 +10,14 @@ declare namespace createError {
statusCode?: number;
}

export interface FastifyErrorConstructor {
new(a?: any, b?: any, c?: any): FastifyError;
(a?: any, b?: any, c?: any): FastifyError;
readonly prototype: FastifyError;
export interface FastifyErrorConstructor<E extends {} = {}> {
new (a?: any, b?: any, c?: any): FastifyError & E;
(a?: any, b?: any, c?: any): FastifyError & E;
readonly prototype: FastifyError & E;
}

export const createError: CreateError
export { createError as default }
}

declare function createError(...params: Parameters<CreateError>): ReturnType<CreateError>
export = createError
16 changes: 12 additions & 4 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ import createError, { FastifyError, FastifyErrorConstructor } from '..'
import { expectType } from 'tsd'

const CustomError = createError('ERROR_CODE', 'message')
expectType<FastifyErrorConstructor>(CustomError)
expectType<FastifyErrorConstructor<{ code: 'ERROR_CODE' }>>(CustomError)
const err = new CustomError()
expectType<FastifyError>(err)
expectType<string>(err.code)
expectType<FastifyError & { code: 'ERROR_CODE' }>(err)
expectType<'ERROR_CODE'>(err.code)
expectType<string>(err.message)
expectType<number>(err.statusCode!)
expectType<number>(err.statusCode!)

const CustomTypedError = createError('OTHER_CODE', 'message', 400)
expectType<FastifyErrorConstructor<{ code: 'OTHER_CODE'; statusCode: 400 }>>(CustomTypedError)
const typed = new CustomTypedError()
expectType<FastifyError & { code: 'OTHER_CODE'; statusCode: 400 }>(typed)
expectType<'OTHER_CODE'>(typed.code)
expectType<string>(typed.message)
expectType<400>(typed.statusCode)