Skip to content
This repository has been archived by the owner on Dec 19, 2022. It is now read-only.

Commit

Permalink
make createRouter() stricter (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT authored Jan 22, 2022
1 parent bd89a1a commit c104f08
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
17 changes: 16 additions & 1 deletion src/server.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expectTypeOf } from 'expect-type';
import { z } from 'zod';
import { appRouter } from './server';
import { inferProcedure } from './trpc/server';
import { inferProcedure, initTRPC } from './trpc/server';

///////////// this below are just tests that the type checking is doing it's thing right ////////////
async function main() {
Expand Down Expand Up @@ -59,5 +59,20 @@ async function main() {
lengthOf: number;
}>();
}
{
// no leaky
const trpc = initTRPC();
trpc.router({
queries: {
test: () => {
return {
data: 'ok',
};
},
},
// @ts-expect-error should error
doesNotExist: {},
});
}
}
main();
11 changes: 9 additions & 2 deletions src/trpc/server/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ export interface ProceduresByType<TContext> {
mutations?: ProcedureRecord<TContext>;
}

type ValidateShape<TActualShape, TExpectedShape> =
TActualShape extends TExpectedShape
? Exclude<keyof TActualShape, keyof TExpectedShape> extends never
? TActualShape
: TExpectedShape
: never;

export function createRouterWithContext<TContext>() {
return function createRouter<TProcedures extends ProceduresByType<TContext>>(
procedures: TProcedures,
procedures: ValidateShape<TProcedures, ProceduresByType<TContext>>,
): TProcedures {
return procedures;
return procedures as any;
};
}

0 comments on commit c104f08

Please sign in to comment.