Skip to content

Commit

Permalink
bug(server): input matching { description?: string } breaks built p…
Browse files Browse the repository at this point in the history
…rocedure (#5297)


---------

Co-authored-by: Alex / KATT <alexander@n1s.se>
  • Loading branch information
juliusmarminge and KATT committed Jan 12, 2024
1 parent fc3e467 commit 65d8afa
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
13 changes: 7 additions & 6 deletions examples/.test/internal-types-export/src/server.ts
Expand Up @@ -16,11 +16,12 @@ const someMiddleware = t.middleware(({ next }) => {
return next();
});

export function genericRouter<TSchema extends (value: any) => unknown>(schema: TSchema) {
return t.router({
foo: t.procedure.output(schema).query(() => 'bar'),
});
}
// Note, this is not valid in v11: https://github.com/trpc/trpc/pull/3831#discussion_r1450437174
// export function genericRouter<TSchema extends (value: any) => unknown>(schema: TSchema) {
// return t.router({
// foo: t.procedure.output(schema).query(() => 'bar'),
// });
// }

export type Foo = { x: Foo | number };

Expand All @@ -34,7 +35,7 @@ const routerB = t.router({
const appRouter = t.router({
foo: t.procedure.query(() => 'bar'),
hello: t.procedure.use(someMiddleware).query(() => 'hello'),
generic: genericRouter((value: string) => value.toUpperCase()),
// generic: genericRouter((value: string) => value.toUpperCase()),
merged: t.mergeRouters(routerA, routerB),
recursive: t.procedure.query(() => {
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/procedureBuilder.ts
Expand Up @@ -34,7 +34,7 @@ type ErrorMessage<TMessage extends string> = TMessage;
/** @internal */
export const unsetMarker = Symbol('unsetMarker');
type UnsetMarker = typeof unsetMarker;
type DefaultValue<TValue, TFallback> = UnsetMarker extends TValue
type DefaultValue<TValue, TFallback> = TValue extends UnsetMarker
? TFallback
: TValue;

Expand Down
@@ -0,0 +1,49 @@
import type { QueryProcedure } from '@trpc/core';
import { initTRPC } from '@trpc/server';
import * as z from 'zod';

describe('Serialization of Record types', () => {
const t = initTRPC.create();
const appRouter = t.router({
withDescriptionKey: t.procedure
.input(
z.object({
description: z.string(),
}),
)
.query(({ input }) => {
expectTypeOf<{ description: string }>(input);
return { input };
}),
maybeDescriptionKey: t.procedure
.input(
z.object({
description: z.string().optional(),
}),
)
.query(({ input }) => {
expectTypeOf<{ description?: string }>(input);
return { input };
}),
});

test("Description key doesn't get matched on unsetMarker", async () => {
expectTypeOf<
QueryProcedure<{
input: {
description: string;
};
output: { input: { description: string } };
}>
>(appRouter.withDescriptionKey);

expectTypeOf<
QueryProcedure<{
input: {
description?: string | undefined;
};
output: { input: { description?: string | undefined } };
}>
>(appRouter.maybeDescriptionKey);
});
});

2 comments on commit 65d8afa

@vercel
Copy link

@vercel vercel bot commented on 65d8afa Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

trpc-next-app-dir – ./examples/.experimental/next-app-dir

trpc-next-app-dir-git-next-trpc.vercel.app
trpc-next-app-dir-trpc.vercel.app
trpc-next-app-dir.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 65d8afa Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.