Skip to content

Commit df518d6

Browse files
committed
🔧 fix: 1108
1 parent 4ba6085 commit df518d6

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.2.25 - 6 Mar 2025
2+
Bug fix:
3+
- [#1108](https://github.com/elysiajs/elysia/issues/1108) use validation response instead of return type when schema is provided
4+
15
# 1.2.24 - 2 Mar 2025
26
Bug fix:
37
- 200 object response is not inferring type in type-level

src/types.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,12 +1537,16 @@ export type EmptyRouteSchema = {
15371537

15381538
type _ComposeElysiaResponse<Schema extends RouteSchema, Handle> = Prettify<
15391539
Prettify<
1540-
{
1541-
200: Exclude<Handle, ElysiaCustomStatusResponse<any, any, any>>
1542-
} & ExtractErrorFromHandle<Handle> &
1543-
({} extends Schema['response']
1544-
? {}
1545-
: Omit<Schema['response'], 200>) &
1540+
(Schema['response'] extends { 200: any }
1541+
? {}
1542+
: {
1543+
200: Exclude<
1544+
Handle,
1545+
ElysiaCustomStatusResponse<any, any, any>
1546+
>
1547+
}) &
1548+
ExtractErrorFromHandle<Handle> &
1549+
({} extends Schema['response'] ? {} : Schema['response']) &
15461550
(EmptyRouteSchema extends Schema
15471551
? {}
15481552
: {

test/types/index.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,3 +2246,55 @@ type a = keyof {}
22462246
}
22472247
})
22482248
}
2249+
2250+
// Use validation response instead of return type
2251+
{
2252+
const app = new Elysia().get(
2253+
'/',
2254+
() => {
2255+
return {
2256+
name: 'a',
2257+
a: 'b'
2258+
}
2259+
},
2260+
{
2261+
response: {
2262+
200: t.Object({
2263+
name: t.String()
2264+
}),
2265+
400: t.Object({
2266+
name: t.String()
2267+
})
2268+
}
2269+
}
2270+
)
2271+
2272+
expectTypeOf<(typeof app._routes.index.get.response)[200]>().toEqualTypeOf<{
2273+
name: string
2274+
}>()
2275+
}
2276+
2277+
// Use return type when validation is not provided
2278+
{
2279+
const app = new Elysia().get(
2280+
'/',
2281+
() => {
2282+
return {
2283+
name: 'a',
2284+
a: 'b'
2285+
}
2286+
},
2287+
{
2288+
response: {
2289+
400: t.Object({
2290+
name: t.String()
2291+
})
2292+
}
2293+
}
2294+
)
2295+
2296+
expectTypeOf<(typeof app._routes.index.get.response)[200]>().toEqualTypeOf<{
2297+
name: string
2298+
a: string
2299+
}>()
2300+
}

0 commit comments

Comments
 (0)