Skip to content

Commit

Permalink
types: check error argument type (#98)
Browse files Browse the repository at this point in the history
* update

* check arg type

* remove max len limit on arg type

* type

* generic argument can look better

* space

* run prettier

* run prettier

* prettier: semi false

* code review

* run lint

* run lint

* run ts-standard

* fix
  • Loading branch information
trim21 authored May 9, 2023
1 parent 975b275 commit b58dbcf
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
31 changes: 26 additions & 5 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
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 }>
declare function createError<C extends string, SC extends number, Arg extends unknown[] = [any?, any?, any?]> (
code: C,
message: string,
statusCode: SC,
Base?: Error
): createError.FastifyErrorConstructor<{ code: C, statusCode: SC }, Arg>

declare function createError<C extends string, Arg extends unknown[] = [any?, any?, any?]> (
code: C,
message: string,
statusCode?: number,
Base?: Error
): createError.FastifyErrorConstructor<{ code: C }, Arg>

declare function createError<Arg extends unknown[] = [any?, any?, any?]> (
code: string,
message: string,
statusCode?: number,
Base?: Error
): createError.FastifyErrorConstructor<{ code: string }, Arg>

type CreateError = typeof createError

Expand All @@ -10,9 +28,12 @@ declare namespace createError {
statusCode?: number
}

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
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
}

Expand Down
51 changes: 50 additions & 1 deletion types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -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<FastifyErrorConstructor<{ code: 'ERROR_CODE' }>>(CustomError)
Expand All @@ -16,3 +16,52 @@ expectType<FastifyError & { code: 'OTHER_CODE', statusCode: 400 }>(typed)
expectType<'OTHER_CODE'>(typed.code)
expectType<string>(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<string, number, [string]>('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<string, number, [string, string]>('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<string, number, [string, string]>('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<string, number, [string, string, string, string]>('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'))

0 comments on commit b58dbcf

Please sign in to comment.