Repository navigation
Commit 13d572f
authored
fix(contract): keep schema constraint when inferring types of a generic schema (#2455)
## Summary
Fixes a type regression from
#2451. `InferSchemaInput` /
`InferSchemaOutput` were a non-distributive conditional (`0 extends 1 &
T ? unknown : ...`). For a type parameter `T`, TypeScript defers it and
uses `unknown` as the constraint, so the schema's constraint was lost:
```ts
function crud<T extends Schema<{ id: string }>>(s: T) {
return os.input(s).handler(({ input }) => input.id) // TS2339 on main
}
```
It was also unsound: `function f<T extends AnySchema>(x: unknown):
InferSchemaInput<T> { return x }` compiled.
Wrapping it as `T extends unknown ? (0 extends 1 & T ? unknown :
NonNullable<T['~standard']['types']>['input']) : never` makes it
distributive. TypeScript then computes the deferred type's constraint
from `T`'s constraint again. Concrete schemas resolve the same as
before, and the `any` guard still gives `unknown`.
## Changes
- `packages/contract/src/schema.ts`: the distributive wrapper, for both
input and output.
- `packages/server/src/procedure-client.ts`: the two `as any` casts that
#2451 added are back to `as typeof output`. They no longer fail to
type-check.
- Type tests:
- The generic-schema pattern for both helpers, including an
`@ts-expect-error` for the unsound assignment.
- `os.input(genericSchema).handler(...)` in `builder.test-d.ts`.
- `types: undefined` → `never`, which pins down the behavior #2451
documented.
The new generic tests fail on `main` (TS2339 / TS2322 / unused
`@ts-expect-error`) and pass with this change.
## Measurements
I couldn't use orpc-benchmarks from this session, so I generated a
similar project locally: 3,000 procedures, each with
`.input(...).output(...).handler(...)`, plus a typed
`createRouterClient` call for each. I ran `tsc --singleThreaded
--extendedDiagnostics` with TypeScript 7.0.2, against the workspace
sources with only `schema.ts` swapped. "Before #2451" is the `infer`
conditional. Counts are deterministic, and every run had 0 errors.
| Inputs declared with | Before #2451 | `main` | This PR | vs `main` |
| :-- | --: | --: | --: | --: |
| Vendored Standard Schema interface | 1,159,108 | 887,337 | 899,392 |
+1.4% |
| Zod 4.6.7 | 3,469,514 | 2,987,743 | 2,999,798 | +0.4% |
| Valibot 1.5.0 | 2,795,503 | 2,193,732 | 2,205,787 | +0.5% |
| oRPC's `Schema<I, O>` | 889,071 | 893,338 | 905,393 | +1.4% |
- **Extra instantiations:** +12,055 in every row, about 4 per procedure.
Type counts are 17 lower than `main` in every row.
- **#2451's savings:** almost all kept. Against the `infer` form, the
vendored interface goes from −23.4% to −22.4%, Zod from −13.9% to
−13.5%, and Valibot from −21.5% to −21.1%.
- **`Schema<I, O>`:** this was already the one row where #2451 cost
more. Against the `infer` form, it goes from +0.5% to +1.8%.
## Verification
- `pnpm check` passes.
- `pnpm test` passes: 4585 tests, plus the bun and cloudflare suites.1 parent 0c48eda commit 13d572f
4 files changed
Lines changed: 63 additions & 10 deletions
File tree
- packages
- contract/src
- server/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
24 | 32 | | |
25 | 33 | | |
26 | 34 | | |
| |||
50 | 58 | | |
51 | 59 | | |
52 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
53 | 76 | | |
54 | 77 | | |
55 | 78 | | |
| |||
61 | 84 | | |
62 | 85 | | |
63 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
64 | 102 | | |
65 | 103 | | |
66 | 104 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
24 | | - | |
25 | | - | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
26 | 28 | | |
27 | 29 | | |
28 | 30 | | |
29 | 31 | | |
30 | 32 | | |
31 | 33 | | |
32 | | - | |
33 | | - | |
34 | | - | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
35 | 39 | | |
36 | 40 | | |
37 | 41 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
290 | 290 | | |
291 | 291 | | |
292 | 292 | | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
293 | 303 | | |
294 | 304 | | |
295 | 305 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
154 | 154 | | |
155 | 155 | | |
156 | 156 | | |
157 | | - | |
| 157 | + | |
158 | 158 | | |
159 | 159 | | |
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
163 | | - | |
164 | | - | |
165 | | - | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
166 | 167 | | |
167 | 168 | | |
168 | 169 | | |
| |||
0 commit comments