Skip to content

Fixing types recognition in elysia.route and hook handlers (before and after) #738

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 5 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ import type {
HigherOrderFunction,
ResolvePath,
JoinPath,
ValidatorLayer
ValidatorLayer,
RouteConfig
} from './types'

export type AnyElysia = Elysia<any, any, any, any, any, any, any, any>
Expand Down Expand Up @@ -437,7 +438,7 @@ export default class Elysia<
path: string,
handle: Handler<any, any, any> | any,
localHook?: LocalHook<any, any, any, any, any, any>,
{ allowMeta = false, skipPrefix = false } = {
{ allowMeta = false, skipPrefix = false }: RouteConfig = {
allowMeta: false as boolean | undefined,
skipPrefix: false as boolean | undefined
}
Expand Down Expand Up @@ -4522,11 +4523,8 @@ export default class Elysia<
Definitions['error'],
Metadata['macro'],
JoinPath<BasePath, Path>
> & {
config: {
allowMeta?: boolean
}
}
> &
Partial<{ config: RouteConfig }>
): Elysia<
BasePath,
Scoped,
Expand Down
9 changes: 7 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ export interface DefinitionBase {

export type RouteBase = Record<string, unknown>

export interface RouteConfig {
allowMeta?: boolean | undefined
skipPrefix?: boolean | undefined
}

export interface MetadataBase {
schema: RouteSchema
macro: BaseMacro
Expand Down Expand Up @@ -640,7 +645,7 @@ export type OptionalHandler<
Path extends string = ''
> =
Handler<Route, Singleton, Path> extends (
context: infer Context
context: Context<Route, Singleton, Path>
) => infer Returned
? (context: Context) => Returned | MaybePromise<void>
: never
Expand All @@ -656,7 +661,7 @@ export type AfterHandler<
Path extends string = ''
> =
Handler<Route, Singleton, Path> extends (
context: infer Context
context: Context<Route, Singleton, Path>
) => infer Returned
? (
context: Prettify<
Expand Down
18 changes: 13 additions & 5 deletions test/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ app.group(
'/:a',
{
beforeHandle({ params, params: { a } }) {
// @ts-expect-error
expectTypeOf<typeof params>().toEqualTypeOf<{
a: string
}>()
Expand Down Expand Up @@ -754,6 +755,7 @@ app.group(
'/:c',
{
beforeHandle({ params, params: { a, c } }) {
// @ts-expect-error
expectTypeOf<typeof params>().toEqualTypeOf<{
a: string
c: string
Expand Down Expand Up @@ -785,6 +787,7 @@ app.group(
user: t.String()
}),
beforeHandle: ({ body }) => {
// @ts-expect-error
expectTypeOf<typeof body>().toEqualTypeOf<{
username: string
}>()
Expand All @@ -795,10 +798,12 @@ app.group(
'/:c',
{
beforeHandle({ body, query }) {
// @ts-expect-error
expectTypeOf<typeof body>().toEqualTypeOf<{
password: string
}>()

// @ts-expect-error
expectTypeOf<typeof query>().toEqualTypeOf<{
user: string
}>()
Expand Down Expand Up @@ -992,6 +997,7 @@ app.group(
id: t.Numeric()
}),
beforeHandle({ params }) {
// @ts-expect-error
expectTypeOf<typeof params>().toEqualTypeOf<{
id: number
}>()
Expand Down Expand Up @@ -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<typeof authorization>().toBeString()
Expand Down Expand Up @@ -1106,6 +1113,7 @@ const a = app.resolve(({ headers }) => {
.onBeforeHandle((context) => {
expectTypeOf<
'b' extends keyof typeof context ? true : false
// @ts-expect-error
>().toEqualTypeOf<true>()
})

Expand Down