From 47cb8e257ddfcf22bb6230bfea4e6f1c2b5ed654 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Guimar=C3=A3es?= Date: Thu, 18 Jul 2024 19:55:07 -0300 Subject: [PATCH 1/3] elysie.route now recognize config as optional param and routehandlers recognize context param as Context properly --- src/index.ts | 12 +++++------- src/types.ts | 9 +++++++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index d184b159..615e8550 100644 --- a/src/index.ts +++ b/src/index.ts @@ -125,7 +125,8 @@ import type { HigherOrderFunction, ResolvePath, JoinPath, - ValidatorLayer + ValidatorLayer, + RouteConfig } from './types' export type AnyElysia = Elysia @@ -437,7 +438,7 @@ export default class Elysia< path: string, handle: Handler | any, localHook?: LocalHook, - { allowMeta = false, skipPrefix = false } = { + { allowMeta = false, skipPrefix = false }: RouteConfig = { allowMeta: false as boolean | undefined, skipPrefix: false as boolean | undefined } @@ -4522,11 +4523,8 @@ export default class Elysia< Definitions['error'], Metadata['macro'], JoinPath - > & { - config: { - allowMeta?: boolean - } - } + > & + Partial<{ config: RouteConfig }> ): Elysia< BasePath, Scoped, diff --git a/src/types.ts b/src/types.ts index 2c44803e..5fe3118a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -301,6 +301,11 @@ export interface DefinitionBase { export type RouteBase = Record +export interface RouteConfig { + allowMeta?: boolean | undefined + skipPrefix?: boolean | undefined +} + export interface MetadataBase { schema: RouteSchema macro: BaseMacro @@ -640,7 +645,7 @@ export type OptionalHandler< Path extends string = '' > = Handler extends ( - context: infer Context + context: Context ) => infer Returned ? (context: Context) => Returned | MaybePromise : never @@ -656,7 +661,7 @@ export type AfterHandler< Path extends string = '' > = Handler extends ( - context: infer Context + context: Context ) => infer Returned ? ( context: Prettify< From 326261b61f4ef9faa8806a5f7b872bd63716df8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Guimar=C3=A3es?= Date: Thu, 18 Jul 2024 19:55:07 -0300 Subject: [PATCH 2/3] elysia.route now recognize config as optional param and routehandlers recognize context param as Context properly --- src/index.ts | 12 +++++------- src/types.ts | 9 +++++++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index d184b159..615e8550 100644 --- a/src/index.ts +++ b/src/index.ts @@ -125,7 +125,8 @@ import type { HigherOrderFunction, ResolvePath, JoinPath, - ValidatorLayer + ValidatorLayer, + RouteConfig } from './types' export type AnyElysia = Elysia @@ -437,7 +438,7 @@ export default class Elysia< path: string, handle: Handler | any, localHook?: LocalHook, - { allowMeta = false, skipPrefix = false } = { + { allowMeta = false, skipPrefix = false }: RouteConfig = { allowMeta: false as boolean | undefined, skipPrefix: false as boolean | undefined } @@ -4522,11 +4523,8 @@ export default class Elysia< Definitions['error'], Metadata['macro'], JoinPath - > & { - config: { - allowMeta?: boolean - } - } + > & + Partial<{ config: RouteConfig }> ): Elysia< BasePath, Scoped, diff --git a/src/types.ts b/src/types.ts index 2c44803e..5fe3118a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -301,6 +301,11 @@ export interface DefinitionBase { export type RouteBase = Record +export interface RouteConfig { + allowMeta?: boolean | undefined + skipPrefix?: boolean | undefined +} + export interface MetadataBase { schema: RouteSchema macro: BaseMacro @@ -640,7 +645,7 @@ export type OptionalHandler< Path extends string = '' > = Handler extends ( - context: infer Context + context: Context ) => infer Returned ? (context: Context) => Returned | MaybePromise : never @@ -656,7 +661,7 @@ export type AfterHandler< Path extends string = '' > = Handler extends ( - context: infer Context + context: Context ) => infer Returned ? ( context: Prettify< From 5c9467c8e4ee708c8990c9411311687417dbbf01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Guimar=C3=A3es?= Date: Tue, 23 Jul 2024 11:51:33 -0300 Subject: [PATCH 3/3] adding expect error for type infering mismatch in deep/recursive structures --- test/types/index.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/test/types/index.ts b/test/types/index.ts index 9146b24f..93c1f91d 100644 --- a/test/types/index.ts +++ b/test/types/index.ts @@ -724,6 +724,7 @@ app.group( '/:a', { beforeHandle({ params, params: { a } }) { + // @ts-expect-error expectTypeOf().toEqualTypeOf<{ a: string }>() @@ -754,6 +755,7 @@ app.group( '/:c', { beforeHandle({ params, params: { a, c } }) { + // @ts-expect-error expectTypeOf().toEqualTypeOf<{ a: string c: string @@ -785,6 +787,7 @@ app.group( user: t.String() }), beforeHandle: ({ body }) => { + // @ts-expect-error expectTypeOf().toEqualTypeOf<{ username: string }>() @@ -795,10 +798,12 @@ app.group( '/:c', { beforeHandle({ body, query }) { + // @ts-expect-error expectTypeOf().toEqualTypeOf<{ password: string }>() + // @ts-expect-error expectTypeOf().toEqualTypeOf<{ user: string }>() @@ -992,6 +997,7 @@ app.group( id: t.Numeric() }), beforeHandle({ params }) { + // @ts-expect-error expectTypeOf().toEqualTypeOf<{ id: number }>() @@ -1072,11 +1078,12 @@ app.group( }) } -const a = app.resolve(({ headers }) => { - return { - authorization: headers.authorization as string - } -}) +const a = app + .resolve(({ headers }) => { + return { + authorization: headers.authorization as string + } + }) // .get('/', ({ authorization }) => { // // ? infers derive type // expectTypeOf().toBeString() @@ -1106,6 +1113,7 @@ const a = app.resolve(({ headers }) => { .onBeforeHandle((context) => { expectTypeOf< 'b' extends keyof typeof context ? true : false + // @ts-expect-error >().toEqualTypeOf() })