Repository navigation
Commit f188e56
authored
fix(server): merge union and
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% |any middleware contexts correctly (#2469)1 parent 426e1d0 commit f188e56
3 files changed
Lines changed: 101 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
127 | 127 | | |
128 | 128 | | |
129 | 129 | | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
130 | 164 | | |
131 | 165 | | |
132 | 166 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
47 | 77 | | |
48 | 78 | | |
49 | 79 | | |
| |||
80 | 110 | | |
81 | 111 | | |
82 | 112 | | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
83 | 130 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
16 | 22 | | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
| 23 | + | |
| 24 | + | |
21 | 25 | | |
22 | | - | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
0 commit comments