From b64bb2020dbc47b0c8375ebbd407d3e1013f4fcd Mon Sep 17 00:00:00 2001 From: cm-ayf Date: Sat, 3 Dec 2022 10:43:33 +0900 Subject: [PATCH 1/4] strictly type around code and statusCode --- types/index.d.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index ddaadd3..de322ee 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,4 +1,7 @@ -type CreateError = (code: string, message: string, statusCode?: number, Base?: Error) => createError.FastifyErrorConstructor +declare function createError(code: C, message: string, statusCode: SC, Base?: Error): createError.FastifyErrorConstructor<{ code: C; statusCode: SC }>; +declare function createError(code: C, message: string, statusCode?: number, Base?: Error): createError.FastifyErrorConstructor<{ code: C; }>; + +type CreateError = typeof createError; declare namespace createError { export interface FastifyError extends Error { @@ -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 { + 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): ReturnType export = createError From 59166abe68c3c557fd6cb2b596fbf0078d2b4660 Mon Sep 17 00:00:00 2001 From: cm-ayf Date: Sat, 3 Dec 2022 10:46:13 +0900 Subject: [PATCH 2/4] add test around strictly typed code and statusCode --- types/index.test-d.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 54cd073..6066602 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -7,4 +7,12 @@ const err = new CustomError() expectType(err) expectType(err.code) expectType(err.message) -expectType(err.statusCode!) \ No newline at end of file +expectType(err.statusCode!) + +const CustomTypedError = createError('OTHER_CODE', 'message', 400) +expectType>(CustomTypedError) +const typed = new CustomTypedError() +expectType(typed) +expectType<'OTHER_CODE'>(typed.code) +expectType(typed.message) +expectType<400>(typed.statusCode) From 2b7c57d5335d0c94a99218260485b42f6a07a2aa Mon Sep 17 00:00:00 2001 From: cm-ayf Date: Sat, 3 Dec 2022 10:48:10 +0900 Subject: [PATCH 3/4] narrower type to test --- types/index.test-d.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 6066602..37d7fc3 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -2,10 +2,10 @@ import createError, { FastifyError, FastifyErrorConstructor } from '..' import { expectType } from 'tsd' const CustomError = createError('ERROR_CODE', 'message') -expectType(CustomError) +expectType>(CustomError) const err = new CustomError() -expectType(err) -expectType(err.code) +expectType(err) +expectType<'ERROR_CODE'>(err.code) expectType(err.message) expectType(err.statusCode!) From 5319190cfec04992aece6f0976e27632cda669b5 Mon Sep 17 00:00:00 2001 From: cm-ayf Date: Wed, 7 Dec 2022 20:36:16 +0900 Subject: [PATCH 4/4] stricter generics constraint --- types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index de322ee..9591a93 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -10,7 +10,7 @@ declare namespace createError { statusCode?: number; } - export interface FastifyErrorConstructor { + export interface FastifyErrorConstructor { new (a?: any, b?: any, c?: any): FastifyError & E; (a?: any, b?: any, c?: any): FastifyError & E; readonly prototype: FastifyError & E;