Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix lint script on types #99

Merged
merged 1 commit into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"types": "types/index.d.ts",
"scripts": {
"lint": "standard",
"lint": "ts-standard",
"test": "npm run test:unit && npm run test:typescript",
"test:unit": "tap",
"test:typescript": "tsd"
Expand All @@ -28,8 +28,8 @@
"homepage": "https://github.com/fastify/fastify-error#readme",
"devDependencies": {
"benchmark": "^2.1.4",
"standard": "^17.0.0",
"tap": "^16.0.0",
"ts-standard": "^12.0.2",
"tsd": "^0.28.0"
},
"tsd": {
Expand Down
6 changes: 6 additions & 0 deletions tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true
}
}
20 changes: 10 additions & 10 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
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> (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 }>

type CreateError = typeof createError;
type CreateError = typeof createError

declare namespace createError {
export interface FastifyError extends Error {
code: string;
name: string;
statusCode?: number;
code: string
name: string
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;
readonly prototype: FastifyError & E;
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
readonly prototype: FastifyError & E
}

export const createError: CreateError
Expand Down
6 changes: 3 additions & 3 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const err = new CustomError()
expectType<FastifyError & { code: 'ERROR_CODE' }>(err)
expectType<'ERROR_CODE'>(err.code)
expectType<string>(err.message)
expectType<number>(err.statusCode!)
expectType<number | undefined>(err.statusCode)

const CustomTypedError = createError('OTHER_CODE', 'message', 400)
expectType<FastifyErrorConstructor<{ code: 'OTHER_CODE'; statusCode: 400 }>>(CustomTypedError)
expectType<FastifyErrorConstructor<{ code: 'OTHER_CODE', statusCode: 400 }>>(CustomTypedError)
const typed = new CustomTypedError()
expectType<FastifyError & { code: 'OTHER_CODE'; statusCode: 400 }>(typed)
expectType<FastifyError & { code: 'OTHER_CODE', statusCode: 400 }>(typed)
expectType<'OTHER_CODE'>(typed.code)
expectType<string>(typed.message)
expectType<400>(typed.statusCode)