Skip to content

Commit

Permalink
Strictly type createError (#89)
Browse files Browse the repository at this point in the history
* strictly type around code and statusCode

* add test around strictly typed code and statusCode

* narrower type to test

* stricter generics constraint
  • Loading branch information
cm-ayf authored Dec 7, 2022
1 parent caa1c8b commit 0569016
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
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 { code: string; statusCode?: number } = { code: string; statusCode?: number }> {
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)

0 comments on commit 0569016

Please sign in to comment.