Skip to content

Commit

Permalink
fix: type of base argument create error (#108)
Browse files Browse the repository at this point in the history
* Fix type of Base argument to createError

* Update README for fix to type of Base arg

* Use ErrorConstructor for type of Base

* Update README
  • Loading branch information
jessta authored Jun 29, 2023
1 parent 514cb8f commit 5b4f5df
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ createError(code, message [, statusCode [, Base]])
- `code` (`string`, required) - The error code, you can access it later with `error.code`. For consistency, we recommend prefixing plugin error codes with `FST_`
- `message` (`string`, required) - The error message. You can also use interpolated strings for formatting the message.
- `statusCode` (`number`, optional) - The status code that Fastify will use if the error is sent via HTTP.
- `Base` (`Error`, optional) - The base error object that will be used. (eg `TypeError`, `RangeError`)
- `Base` (`ErrorConstructor`, optional) - The base error object that will be used. (eg `TypeError`, `RangeError`)

```js
const createError = require('@fastify/error')
Expand Down
6 changes: 3 additions & 3 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ declare function createError<C extends string, SC extends number, Arg extends un
code: C,
message: string,
statusCode: SC,
Base?: Error
Base?: ErrorConstructor
): 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
Base?: ErrorConstructor
): createError.FastifyErrorConstructor<{ code: C }, Arg>

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

type CreateError = typeof createError
Expand Down
4 changes: 4 additions & 0 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ expectError(new CustomTypedArgError6('a', 'b'))
expectError(new CustomTypedArgError6('a', 'b', 'c'))
CustomTypedArgError6('a', 'b', 'c', 'd')
expectError(new CustomTypedArgError6('a', 'b', 'c', 'd', 'e'))


const CustomErrorWithErrorConstructor = createError('ERROR_CODE', 'message', 500, TypeError)
expectType<FastifyErrorConstructor<{ code: 'ERROR_CODE', statusCode: 500 }>>(CustomErrorWithErrorConstructor)

0 comments on commit 5b4f5df

Please sign in to comment.