diff --git a/types/index.d.ts b/types/index.d.ts index 2783e7d..3dd0873 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,5 +1,23 @@ -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 }> +declare function createError ( + code: C, + message: string, + statusCode: SC, + Base?: Error +): createError.FastifyErrorConstructor<{ code: C, statusCode: SC }, Arg> + +declare function createError ( + code: C, + message: string, + statusCode?: number, + Base?: Error +): createError.FastifyErrorConstructor<{ code: C }, Arg> + +declare function createError ( + code: string, + message: string, + statusCode?: number, + Base?: Error +): createError.FastifyErrorConstructor<{ code: string }, Arg> type CreateError = typeof createError @@ -10,9 +28,12 @@ declare namespace createError { statusCode?: number } - export interface FastifyErrorConstructor { - new (a?: any, b?: any, c?: any): FastifyError & E - (a?: any, b?: any, c?: any): FastifyError & E + export interface FastifyErrorConstructor< + E extends { code: string, statusCode?: number } = { code: string, statusCode?: number }, + T extends unknown[] = [any?, any?, any?] + > { + new(...arg: T): FastifyError & E + (...arg: T): FastifyError & E readonly prototype: FastifyError & E } diff --git a/types/index.test-d.ts b/types/index.test-d.ts index f31aa35..a31a4b0 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -1,5 +1,5 @@ import createError, { FastifyError, FastifyErrorConstructor } from '..' -import { expectType } from 'tsd' +import { expectType, expectError } from 'tsd' const CustomError = createError('ERROR_CODE', 'message') expectType>(CustomError) @@ -16,3 +16,52 @@ expectType(typed) expectType<'OTHER_CODE'>(typed.code) expectType(typed.message) expectType<400>(typed.statusCode) + +/* eslint-disable no-new */ +const CustomTypedArgError = createError<[string]>('OTHER_CODE', 'expect %s message', 400) +CustomTypedArgError('a') +expectError(CustomTypedArgError('a', 'b')) +expectError(new CustomTypedArgError('a', 'b')) +expectError(CustomTypedArgError(1)) +expectError(new CustomTypedArgError(1)) + +const CustomTypedArgError2 = createError('OTHER_CODE', 'expect %s message', 400) +CustomTypedArgError2('a') +expectError(CustomTypedArgError2('a', 'b')) +expectError(new CustomTypedArgError2('a', 'b')) +expectError(CustomTypedArgError2(1)) +expectError(new CustomTypedArgError2(1)) + +const CustomTypedArgError3 = createError('OTHER_CODE', 'expect %s message but got %s', 400) +expectError(CustomTypedArgError3('a')) +CustomTypedArgError3('a', 'b') +new CustomTypedArgError3('a', 'b') +expectError(CustomTypedArgError3(1)) +expectError(new CustomTypedArgError3(1)) +expectError(new CustomTypedArgError3(1, 2)) +expectError(new CustomTypedArgError3('1', 2)) +expectError(new CustomTypedArgError3(1, '2')) + +const CustomTypedArgError4 = createError('OTHER_CODE', 'expect %s message but got %s', 400) +expectError(CustomTypedArgError4('a')) +CustomTypedArgError4('a', 'b') +new CustomTypedArgError4('a', 'b') +expectError(CustomTypedArgError4(1)) +expectError(new CustomTypedArgError4(1)) +expectError(new CustomTypedArgError4(1, 2)) +expectError(new CustomTypedArgError4('1', 2)) +expectError(new CustomTypedArgError4(1, '2')) + +const CustomTypedArgError5 = createError<[string, string, string, string]>('OTHER_CODE', 'expect %s message but got %s. Please contact %s by emailing to %s', 400) +expectError(CustomTypedArgError5('a')) +expectError(new CustomTypedArgError5('a', 'b')) +expectError(new CustomTypedArgError5('a', 'b', 'c')) +CustomTypedArgError5('a', 'b', 'c', 'd') +expectError(new CustomTypedArgError5('a', 'b', 'c', 'd', 'e')) + +const CustomTypedArgError6 = createError('OTHER_CODE', 'expect %s message but got %s. Please contact %s by emailing to %s', 400) +expectError(CustomTypedArgError6('a')) +expectError(new CustomTypedArgError6('a', 'b')) +expectError(new CustomTypedArgError6('a', 'b', 'c')) +CustomTypedArgError6('a', 'b', 'c', 'd') +expectError(new CustomTypedArgError6('a', 'b', 'c', 'd', 'e'))