Skip to content

Commit 0569016

Browse files
authored
Strictly type createError (#89)
* strictly type around code and statusCode * add test around strictly typed code and statusCode * narrower type to test * stricter generics constraint
1 parent caa1c8b commit 0569016

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

types/index.d.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
type CreateError = (code: string, message: string, statusCode?: number, Base?: Error) => createError.FastifyErrorConstructor
1+
declare function createError<C extends string, SC extends number>(code: C, message: string, statusCode: SC, Base?: Error): createError.FastifyErrorConstructor<{ code: C; statusCode: SC }>;
2+
declare function createError<C extends string>(code: C, message: string, statusCode?: number, Base?: Error): createError.FastifyErrorConstructor<{ code: C; }>;
3+
4+
type CreateError = typeof createError;
25

36
declare namespace createError {
47
export interface FastifyError extends Error {
@@ -7,15 +10,14 @@ declare namespace createError {
710
statusCode?: number;
811
}
912

10-
export interface FastifyErrorConstructor {
11-
new(a?: any, b?: any, c?: any): FastifyError;
12-
(a?: any, b?: any, c?: any): FastifyError;
13-
readonly prototype: FastifyError;
13+
export interface FastifyErrorConstructor<E extends { code: string; statusCode?: number } = { code: string; statusCode?: number }> {
14+
new (a?: any, b?: any, c?: any): FastifyError & E;
15+
(a?: any, b?: any, c?: any): FastifyError & E;
16+
readonly prototype: FastifyError & E;
1417
}
1518

1619
export const createError: CreateError
1720
export { createError as default }
1821
}
1922

20-
declare function createError(...params: Parameters<CreateError>): ReturnType<CreateError>
2123
export = createError

types/index.test-d.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@ import createError, { FastifyError, FastifyErrorConstructor } from '..'
22
import { expectType } from 'tsd'
33

44
const CustomError = createError('ERROR_CODE', 'message')
5-
expectType<FastifyErrorConstructor>(CustomError)
5+
expectType<FastifyErrorConstructor<{ code: 'ERROR_CODE' }>>(CustomError)
66
const err = new CustomError()
7-
expectType<FastifyError>(err)
8-
expectType<string>(err.code)
7+
expectType<FastifyError & { code: 'ERROR_CODE' }>(err)
8+
expectType<'ERROR_CODE'>(err.code)
99
expectType<string>(err.message)
10-
expectType<number>(err.statusCode!)
10+
expectType<number>(err.statusCode!)
11+
12+
const CustomTypedError = createError('OTHER_CODE', 'message', 400)
13+
expectType<FastifyErrorConstructor<{ code: 'OTHER_CODE'; statusCode: 400 }>>(CustomTypedError)
14+
const typed = new CustomTypedError()
15+
expectType<FastifyError & { code: 'OTHER_CODE'; statusCode: 400 }>(typed)
16+
expectType<'OTHER_CODE'>(typed.code)
17+
expectType<string>(typed.message)
18+
expectType<400>(typed.statusCode)

0 commit comments

Comments
 (0)