Skip to content

Commit f188e56

Browse files
authored
fix(server): merge union and any middleware contexts correctly (#2469)
Fixes two middleware context typing bugs that are still on `main` (#2458 didn't cover them). ### 1. `MergedContext` with a union or `any` out context `keyof TOutContext extends never` is true for a union whose members share no keys, so the out context was dropped: ```ts os.$context<{ db: string }>() .use(({ next }) => next({ context: s as { user: { id: string } } | { anon: true } })) .handler(({ context }) => {}) // context was only { db: string } ``` `.use(m as Middleware<any, any, …>)` also made the handler context `any`. `MergedContext` now distributes over the out context and returns the current context for `any`. ### 2. `MergedInitialContext` with an in context that requires nothing A middleware whose in context is `Context`, `any` or `Record<string, unknown>` added `{ [k: PropertyKey]: any }` to the initial context. These in contexts are now ignored. The check is `{ [key: PropertyKey]: unknown } extends Required<rest>`, where `rest` is the in context without the keys already provided. It fails for any declared key, optional or not, and for any index signature that restricts values. So `{ a?: string }` and `Record<string, number>` are still kept. Number-, symbol- and template-literal-keyed open records are ignored as well. A simpler `[TInitial] extends [TInContext]` shortcut was rejected because it drops optional keys like `g?: boolean`. ### Not in this PR: `IntersectPick` with a union current context The `.use` out-context constraint (`IntersectPick` in `@orpc/shared`) still only checks keys that every member of a union context shares, so `{ user: 123 }` on `{ kind: 'a'; user: { id: string } } | { kind: 'b' }` is accepted. Earlier commits here tried to fix that (0f87c59 through 2aca330). Each version wrongly rejected some sound override that `main` accepts: narrowing, `next({ context })`, union overrides of different keys, and finally `next({ context: { user: context.user } })` on `{ user: { id: string } } | { user: { email: string } }`. That last one hits a TypeScript limit: `{ user: X | Y }` isn't assignable to `{ user: X } | { user: Y }`. cabb659 restores `IntersectPick` to `main`, and the union check will follow in its own PR with a full test matrix. ### Tests - `packages/server/src/context.test-d.ts`: unit cases for both bugs - `packages/server/src/builder.test-d.ts`: end to end through `.use` for a union out context, an `any` out context, and an in context that requires nothing - Every new assertion fails with `main`'s `context.ts`. - `pnpm check` and `pnpm test` pass. ### Type-check cost Measured with TS 7.0.2, `tsc -p tsconfig.json --extendedDiagnostics --checkers 1`, including the new tests: | | main | this PR | Δ | |---|---|---|---| | Types | 691,135 | 693,451 | +0.3% | | Instantiations | 2,479,982 | 2,474,843 | −0.2% |
1 parent 426e1d0 commit f188e56

3 files changed

Lines changed: 101 additions & 12 deletions

File tree

‎packages/server/src/builder.test-d.ts‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {
1111
BuilderWithMiddlewares,
1212
BuilderWithOutput,
1313
} from './builder-variants'
14-
import type { MergedContext, MergedInitialContext } from './context'
14+
import type { Context, MergedContext, MergedInitialContext } from './context'
1515
import type { Lazy } from './lazy'
1616
import type { Middleware, MiddlewareDone } from './middleware'
1717
import type { DecoratedMiddleware } from './middleware-decorated'
@@ -127,6 +127,40 @@ describe('Builder', () => {
127127
})
128128
})
129129

130+
it('merges union out context', () => {
131+
const out = {} as { user: { id: string } } | { anon: true }
132+
133+
void os
134+
.$context<{ db: string }>()
135+
.use(({ next }) => next({ context: out }))
136+
.handler(({ context }) => {
137+
expectTypeOf(context.db).toEqualTypeOf<string>()
138+
139+
if ('user' in context) {
140+
expectTypeOf(context.user).toEqualTypeOf<{ id: string }>()
141+
} else {
142+
expectTypeOf(context.anon).toEqualTypeOf<true>()
143+
}
144+
})
145+
})
146+
147+
it('ignores any out context', () => {
148+
void os
149+
.$context<{ db: string }>()
150+
.use({} as Middleware<any, any, any, any, any>)
151+
.handler(({ context }) => {
152+
expectTypeOf(context).toEqualTypeOf<{ db: string } & object>()
153+
})
154+
})
155+
156+
it('ignores in context that requires nothing', () => {
157+
expectTypeOf(
158+
os.$context<{ db: string }>().use({} as Middleware<Context, object, unknown, any, object>),
159+
).toEqualTypeOf<
160+
BuilderWithMiddlewares<{ db: string } & object, object, Record<never, never>>
161+
>()
162+
})
163+
130164
it('outline middleware', () => {
131165
const middleware = {} as Middleware<
132166
{ auth: boolean; g?: boolean },

‎packages/server/src/context.test-d.ts‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expectTypeOf, it } from 'vitest'
22

3-
import type { MergedContext, MergedInitialContext } from './context'
3+
import type { Context, MergedContext, MergedInitialContext } from './context'
44

55
describe('MergedInitialContext', () => {
66
it('no extra', () => {
@@ -44,6 +44,36 @@ describe('MergedInitialContext', () => {
4444
MergedInitialContext<object, object, { [k: string]: unknown; db: string }>
4545
>().toEqualTypeOf<object & { [k: string]: unknown; db: string }>()
4646
})
47+
48+
it('ignores in context that requires nothing', () => {
49+
expectTypeOf<MergedInitialContext<{ db: string }, object, Context>>().toEqualTypeOf<{
50+
db: string
51+
}>()
52+
expectTypeOf<MergedInitialContext<{ db: string }, object, any>>().toEqualTypeOf<{
53+
db: string
54+
}>()
55+
expectTypeOf<
56+
MergedInitialContext<{ db: string }, object, Record<string, unknown>>
57+
>().toEqualTypeOf<{ db: string }>()
58+
expectTypeOf<
59+
MergedInitialContext<{ db: string }, object, Record<number, unknown>>
60+
>().toEqualTypeOf<{ db: string }>()
61+
expectTypeOf<
62+
MergedInitialContext<{ db: string }, object, { [k: string]: unknown; db: string }>
63+
>().toEqualTypeOf<{ db: string }>()
64+
})
65+
66+
it('keeps in context that requires something', () => {
67+
expectTypeOf<MergedInitialContext<{ db: string }, object, { a?: string }>>().toEqualTypeOf<
68+
{ db: string } & { a?: string }
69+
>()
70+
expectTypeOf<
71+
MergedInitialContext<{ db: string }, object, { [k: string]: unknown; a?: string }>
72+
>().toEqualTypeOf<{ db: string } & { [k: string]: unknown; a?: string }>()
73+
expectTypeOf<MergedInitialContext<object, object, Record<string, number>>>().toEqualTypeOf<
74+
object & Record<string, number>
75+
>()
76+
})
4777
})
4878

4979
describe('MergedContext', () => {
@@ -80,4 +110,21 @@ describe('MergedContext', () => {
80110
MergedContext<{ [k: string]: unknown; db: string }, { user: number }>
81111
>().toEqualTypeOf<{ [k: string]: unknown; db: string } & { user: number }>()
82112
})
113+
114+
it('merges each member of a union new context', () => {
115+
expectTypeOf<
116+
MergedContext<{ db: string }, { user: { id: string } } | { anon: true }>
117+
>().toEqualTypeOf<
118+
({ db: string } & { user: { id: string } }) | ({ db: string } & { anon: true })
119+
>()
120+
expectTypeOf<
121+
MergedContext<{ db: string; anon: false }, { user: { id: string } } | { anon: true }>
122+
>().toEqualTypeOf<
123+
({ db: string; anon: false } & { user: { id: string } }) | ({ db: string } & { anon: true })
124+
>()
125+
})
126+
127+
it('returns current if new context is any', () => {
128+
expectTypeOf<MergedContext<{ db: string }, any>>().toEqualTypeOf<{ db: string }>()
129+
})
83130
})

‎packages/server/src/context.ts‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,23 @@ export type MergedInitialContext<
88
TInitial extends Context,
99
TOutContext extends Context,
1010
TInContext extends Context,
11-
> = TInContext extends any
12-
? Exclude<keyof TInContext, keyof TInitial | keyof TOutContext> extends never
13-
? TInitial
14-
: TInitial & DistributiveOmit<TInContext, keyof TInitial | keyof TOutContext>
15-
: never
11+
> = 0 extends 1 & TInContext
12+
? TInitial
13+
: TInContext extends any
14+
? Exclude<keyof TInContext, keyof TInitial | keyof TOutContext> extends never
15+
? TInitial
16+
: { [key: PropertyKey]: unknown } extends Required<
17+
DistributiveOmit<TInContext, keyof TInitial | keyof TOutContext>
18+
>
19+
? TInitial
20+
: TInitial & DistributiveOmit<TInContext, keyof TInitial | keyof TOutContext>
21+
: never
1622

17-
export type MergedContext<
18-
TCurrent extends Context,
19-
TOutContext extends Context,
20-
> = keyof TOutContext extends never
23+
export type MergedContext<TCurrent extends Context, TOutContext extends Context> = 0 extends 1 &
24+
TOutContext
2125
? TCurrent
22-
: DistributiveOmit<TCurrent, keyof TOutContext> & TOutContext
26+
: TOutContext extends any
27+
? keyof TOutContext extends never
28+
? TCurrent
29+
: DistributiveOmit<TCurrent, keyof TOutContext> & TOutContext
30+
: never

0 commit comments

Comments
 (0)