Skip to content

Commit a8c405a

Browse files
committed
🔧 fix: recursive macro with conflict value per status
1 parent 6f29e61 commit a8c405a

File tree

3 files changed

+52
-49
lines changed

3 files changed

+52
-49
lines changed

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,7 @@ export type MacroToContext<
10691069
R
10701070
> extends infer A
10711071
? {
1072-
[K in Exclude<keyof A, 'return'>]: A[K]
1072+
[K in Exclude<keyof A, 'return'>]: UnionToIntersect<A[K]>
10731073
} & Prettify<{
10741074
// @ts-ignore
10751075
return: FlattenMacroResponse<A['return']>

test/types/index.ts

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,41 +1223,29 @@ const a = app
12231223

12241224
const app = new Elysia().group('/api', (app) => app.use(testController))
12251225

1226-
expectTypeOf<(typeof app)['~Routes']>().toEqualTypeOf<{
1227-
api: {
1228-
test: {
1229-
'could-be-error': {
1230-
right: {
1231-
get: {
1232-
body: unknown
1233-
params: {}
1234-
query: unknown
1235-
headers: unknown
1236-
response: {
1237-
200: {
1238-
couldBeError: boolean
1239-
}
1240-
}
1241-
}
1242-
}
1243-
}
1244-
}
1245-
} & {
1246-
test: {
1247-
deep: {
1248-
ws: {
1249-
subscribe: {
1250-
body: unknown
1251-
params: {}
1252-
query: unknown
1253-
headers: unknown
1254-
response: {}
1255-
}
1256-
}
1257-
}
1226+
expectTypeOf<
1227+
(typeof app)['~Routes']['api']['test']['could-be-error']['right']['get']
1228+
>().toEqualTypeOf<{
1229+
body: unknown
1230+
params: {}
1231+
query: unknown
1232+
headers: unknown
1233+
response: {
1234+
200: {
1235+
couldBeError: boolean
12581236
}
12591237
}
12601238
}>()
1239+
1240+
expectTypeOf<
1241+
(typeof app)['~Routes']['api']['test']['deep']['ws']['subscribe']
1242+
>().toEqualTypeOf<{
1243+
body: {}
1244+
params: {}
1245+
query: {}
1246+
headers: {}
1247+
response: {}
1248+
}>()
12611249
}
12621250

12631251
// ? Handle error status
@@ -2512,27 +2500,42 @@ type a = keyof {}
25122500

25132501
// onAfterHandle should have response
25142502
{
2515-
new Elysia().onAfterHandle({ as: 'scoped' }, ({ response }) => response)
2503+
new Elysia().onAfterHandle(
2504+
{ as: 'scoped' },
2505+
({ responseValue }) => responseValue
2506+
)
25162507
}
25172508

2509+
/* Neither `a` or `b` exist at the type level, even though they do exist at runtime */
25182510
{
25192511
new Elysia()
25202512
.macro({
25212513
a: {
2514+
body: t.Object({
2515+
a: t.String()
2516+
}),
25222517
resolve: () => ({ a: 'a' as const })
25232518
},
25242519
b: {
2520+
body: t.Object({
2521+
b: t.String()
2522+
}),
25252523
resolve: () => ({ b: 'b' as const })
25262524
}
25272525
})
25282526
.get(
25292527
'/test',
2530-
(
2531-
{
2532-
a,
2533-
b
2534-
} /* Neither `a` or `b` exist at the type level, even though they do exist at runtime */
2535-
) => ({ a, b }),
2528+
({ a, b, body }) => {
2529+
expectTypeOf<typeof body>().toEqualTypeOf<{
2530+
a: string
2531+
b: string
2532+
}>()
2533+
2534+
expectTypeOf<typeof a>().toEqualTypeOf<'a'>()
2535+
expectTypeOf<typeof b>().toEqualTypeOf<'b'>()
2536+
2537+
return { a, b }
2538+
},
25362539
{
25372540
a: true,
25382541
b: true

test/types/lifecycle/soundness.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,29 +2297,29 @@ import { Prettify } from '../../../src/types'
22972297
const app = new Elysia()
22982298
.macro({
22992299
a: {
2300-
resolve: () => ({ a: 'resolved' }) as const,
23012300
beforeHandle({ status }) {
23022301
if (Math.random()) return status(400, 'a')
23032302
if (Math.random()) return status(401, 'a')
23042303

2305-
return 'b'
2304+
return 'before handler' as const
23062305
}
23072306
},
23082307
b: {
2309-
a: true,
2310-
beforeHandle({ status }) {
2308+
resolve({ status }) {
23112309
if (Math.random()) return status(401, 'b')
2312-
}
2310+
},
2311+
a: true
23132312
}
23142313
})
2315-
.get('/', ({ a }) => a, {
2314+
.get('/', () => 'handler' as const, {
23162315
b: true
23172316
})
23182317

2319-
type Route = (typeof app)['~Routes']['get']['response']
23202318

2321-
expectTypeOf<Route>().toEqualTypeOf<{
2322-
200: 'b' | 'resolved'
2319+
type Routes = (typeof app)['~Routes']['get']['response']
2320+
2321+
expectTypeOf<Routes>().toEqualTypeOf<{
2322+
200: 'before handler' | 'handler'
23232323
400: 'a'
23242324
401: 'a' | 'b'
23252325
}>

0 commit comments

Comments
 (0)