Skip to content

Commit 13d572f

Browse files
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/schema.test-d.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ const untypedSchema = {
2121
validate: (value: unknown) => ({ value: value as string }),
2222
},
2323
}
24+
const undefinedTypesSchema = {
25+
'~standard': {
26+
version: 1 as const,
27+
vendor: 'custom',
28+
validate: (value: unknown) => ({ value: value as string }),
29+
types: undefined,
30+
},
31+
}
2432

2533
type UnionSchema = Schema<{ a: number }, { a: string }> | Schema<{ b: number }, { b: string }>
2634

@@ -50,6 +58,21 @@ it('InferSchemaInput', () => {
5058
expectTypeOf<InferSchemaInput<AnySchema>>().toBeAny()
5159
expectTypeOf<InferSchemaInput<any>>().toEqualTypeOf<unknown>()
5260
expectTypeOf<InferSchemaInput<typeof untypedSchema>>().toEqualTypeOf<unknown>()
61+
expectTypeOf<InferSchemaInput<typeof undefinedTypesSchema>>().toEqualTypeOf<never>()
62+
})
63+
64+
it('InferSchemaInput with a generic schema', () => {
65+
function _generic<T extends Schema<{ id: string }>>(value: InferSchemaInput<T>) {
66+
expectTypeOf(value.id).toEqualTypeOf<string>()
67+
68+
const _id: string = value.id
69+
const _value: { id: string } = value
70+
}
71+
72+
function _unsound<T extends AnySchema>(value: unknown): InferSchemaInput<T> {
73+
// @ts-expect-error - unknown is not assignable to an unresolved schema type
74+
return value
75+
}
5376
})
5477

5578
it('InferSchemaOutput', () => {
@@ -61,6 +84,21 @@ it('InferSchemaOutput', () => {
6184
expectTypeOf<InferSchemaOutput<AnySchema>>().toBeAny()
6285
expectTypeOf<InferSchemaOutput<any>>().toEqualTypeOf<unknown>()
6386
expectTypeOf<InferSchemaOutput<typeof untypedSchema>>().toEqualTypeOf<unknown>()
87+
expectTypeOf<InferSchemaOutput<typeof undefinedTypesSchema>>().toEqualTypeOf<never>()
88+
})
89+
90+
it('InferSchemaOutput with a generic schema', () => {
91+
function _generic<T extends Schema<{ id: string }>>(value: InferSchemaOutput<T>) {
92+
expectTypeOf(value.id).toEqualTypeOf<string>()
93+
94+
const _id: string = value.id
95+
const _value: { id: string } = value
96+
}
97+
98+
function _unsound<T extends AnySchema>(value: unknown): InferSchemaOutput<T> {
99+
// @ts-expect-error - unknown is not assignable to an unresolved schema type
100+
return value
101+
}
64102
})
65103

66104
describe('MergedSchema', () => {

‎packages/contract/src/schema.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,22 @@ export type SchemaIssue = StandardSchemaV1.Issue
2020
*
2121
* @see {@link https://orpc.dev/docs/metadata | Metadata}
2222
*/
23-
export type InferSchemaInput<T extends AnySchema> = 0 extends 1 & T
24-
? unknown
25-
: NonNullable<T['~standard']['types']>['input']
23+
export type InferSchemaInput<T extends AnySchema> = T extends unknown
24+
? 0 extends 1 & T
25+
? unknown
26+
: NonNullable<T['~standard']['types']>['input']
27+
: never
2628

2729
/**
2830
* Infers the output type of a schema.
2931
*
3032
* @see {@link https://orpc.dev/docs/metadata | Metadata}
3133
*/
32-
export type InferSchemaOutput<T extends AnySchema> = 0 extends 1 & T
33-
? unknown
34-
: NonNullable<T['~standard']['types']>['output']
34+
export type InferSchemaOutput<T extends AnySchema> = T extends unknown
35+
? 0 extends 1 & T
36+
? unknown
37+
: NonNullable<T['~standard']['types']>['output']
38+
: never
3539

3640
export type MergedSchema<T extends AnySchema, U extends AnySchema> =
3741
T extends Schema<infer TInput, infer TOutput>

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,16 @@ describe('Builder', () => {
290290
>
291291
>()
292292
})
293+
294+
it('generic input schema', () => {
295+
function _crud<T extends Schema<{ id: string }>>(schema: T) {
296+
return os.input(schema).handler(({ input }) => {
297+
expectTypeOf(input.id).toEqualTypeOf<string>()
298+
299+
return input.id
300+
})
301+
}
302+
})
293303
})
294304

295305
it('.router', () => {

‎packages/server/src/procedure-client.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,16 @@ export function createProcedureClient<
154154
: output,
155155
{ mapError: reconcileError },
156156
),
157-
) as any
157+
) as typeof output
158158
} else if (getTracer() && (output as unknown) instanceof ReadableStream) {
159159
/**
160160
* @warning
161161
* Remember use `override` for ReadableStream to remain other special properties
162162
*/
163-
const stream = output as unknown as ReadableStream<unknown>
164-
165-
return override(stream, traceReadableStream('consume_octet_stream_output', stream)) as any
163+
return override(
164+
output,
165+
traceReadableStream('consume_octet_stream_output', output),
166+
) as typeof output
166167
}
167168

168169
return output

0 commit comments

Comments
 (0)