Skip to content

Commit b58dbcf

Browse files
authored
types: check error argument type (#98)
* 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
1 parent 975b275 commit b58dbcf

File tree

2 files changed

+76
-6
lines changed

2 files changed

+76
-6
lines changed

types/index.d.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
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 }>
1+
declare function createError<C extends string, SC extends number, Arg extends unknown[] = [any?, any?, any?]> (
2+
code: C,
3+
message: string,
4+
statusCode: SC,
5+
Base?: Error
6+
): createError.FastifyErrorConstructor<{ code: C, statusCode: SC }, Arg>
7+
8+
declare function createError<C extends string, Arg extends unknown[] = [any?, any?, any?]> (
9+
code: C,
10+
message: string,
11+
statusCode?: number,
12+
Base?: Error
13+
): createError.FastifyErrorConstructor<{ code: C }, Arg>
14+
15+
declare function createError<Arg extends unknown[] = [any?, any?, any?]> (
16+
code: string,
17+
message: string,
18+
statusCode?: number,
19+
Base?: Error
20+
): createError.FastifyErrorConstructor<{ code: string }, Arg>
321

422
type CreateError = typeof createError
523

@@ -10,9 +28,12 @@ declare namespace createError {
1028
statusCode?: number
1129
}
1230

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
31+
export interface FastifyErrorConstructor<
32+
E extends { code: string, statusCode?: number } = { code: string, statusCode?: number },
33+
T extends unknown[] = [any?, any?, any?]
34+
> {
35+
new(...arg: T): FastifyError & E
36+
(...arg: T): FastifyError & E
1637
readonly prototype: FastifyError & E
1738
}
1839

types/index.test-d.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import createError, { FastifyError, FastifyErrorConstructor } from '..'
2-
import { expectType } from 'tsd'
2+
import { expectType, expectError } from 'tsd'
33

44
const CustomError = createError('ERROR_CODE', 'message')
55
expectType<FastifyErrorConstructor<{ code: 'ERROR_CODE' }>>(CustomError)
@@ -16,3 +16,52 @@ expectType<FastifyError & { code: 'OTHER_CODE', statusCode: 400 }>(typed)
1616
expectType<'OTHER_CODE'>(typed.code)
1717
expectType<string>(typed.message)
1818
expectType<400>(typed.statusCode)
19+
20+
/* eslint-disable no-new */
21+
const CustomTypedArgError = createError<[string]>('OTHER_CODE', 'expect %s message', 400)
22+
CustomTypedArgError('a')
23+
expectError(CustomTypedArgError('a', 'b'))
24+
expectError(new CustomTypedArgError('a', 'b'))
25+
expectError(CustomTypedArgError(1))
26+
expectError(new CustomTypedArgError(1))
27+
28+
const CustomTypedArgError2 = createError<string, number, [string]>('OTHER_CODE', 'expect %s message', 400)
29+
CustomTypedArgError2('a')
30+
expectError(CustomTypedArgError2('a', 'b'))
31+
expectError(new CustomTypedArgError2('a', 'b'))
32+
expectError(CustomTypedArgError2(1))
33+
expectError(new CustomTypedArgError2(1))
34+
35+
const CustomTypedArgError3 = createError<string, number, [string, string]>('OTHER_CODE', 'expect %s message but got %s', 400)
36+
expectError(CustomTypedArgError3('a'))
37+
CustomTypedArgError3('a', 'b')
38+
new CustomTypedArgError3('a', 'b')
39+
expectError(CustomTypedArgError3(1))
40+
expectError(new CustomTypedArgError3(1))
41+
expectError(new CustomTypedArgError3(1, 2))
42+
expectError(new CustomTypedArgError3('1', 2))
43+
expectError(new CustomTypedArgError3(1, '2'))
44+
45+
const CustomTypedArgError4 = createError<string, number, [string, string]>('OTHER_CODE', 'expect %s message but got %s', 400)
46+
expectError(CustomTypedArgError4('a'))
47+
CustomTypedArgError4('a', 'b')
48+
new CustomTypedArgError4('a', 'b')
49+
expectError(CustomTypedArgError4(1))
50+
expectError(new CustomTypedArgError4(1))
51+
expectError(new CustomTypedArgError4(1, 2))
52+
expectError(new CustomTypedArgError4('1', 2))
53+
expectError(new CustomTypedArgError4(1, '2'))
54+
55+
const CustomTypedArgError5 = createError<[string, string, string, string]>('OTHER_CODE', 'expect %s message but got %s. Please contact %s by emailing to %s', 400)
56+
expectError(CustomTypedArgError5('a'))
57+
expectError(new CustomTypedArgError5('a', 'b'))
58+
expectError(new CustomTypedArgError5('a', 'b', 'c'))
59+
CustomTypedArgError5('a', 'b', 'c', 'd')
60+
expectError(new CustomTypedArgError5('a', 'b', 'c', 'd', 'e'))
61+
62+
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)
63+
expectError(CustomTypedArgError6('a'))
64+
expectError(new CustomTypedArgError6('a', 'b'))
65+
expectError(new CustomTypedArgError6('a', 'b', 'c'))
66+
CustomTypedArgError6('a', 'b', 'c', 'd')
67+
expectError(new CustomTypedArgError6('a', 'b', 'c', 'd', 'e'))

0 commit comments

Comments
 (0)