Skip to content

Router methods input type narrowing - #1743

Closed
jlalmes wants to merge 22 commits into
mainfrom
feature/input-type-narrowing
Closed

jlalmes wants to merge 22 commits into
mainfrom
feature/input-type-narrowing

Conversation

@jlalmes

@jlalmes jlalmes commented Apr 7, 2022 •

Copy link
Copy Markdown
Contributor

Router methods input type narrowing

Ensure router method TInput type (input:) satisfies TInput type defined in the ProcedureRecord<...> for each TQueries | TMutations | TSubscriptions.

This non-breaking change is required for composing type-safe OpenAPI compliant tRPC routers in a future PR.

// Create a Router that will only allow `input` type of `string` on `router.[method](...)`
const narrowedRouter = new Router<
    TContext,
    TContext,
    TMeta,
    ProcedureRecord<TContext, TContext, TMeta, string, any, any, any>,
    ProcedureRecord<TContext, TContext, TMeta, string, any, any, any>,
    ProcedureRecord<TContext, TContext, TMeta, string, unknown, Subscription<any>, unknown>,
    TErrorShape
  >();

// The following will throw a TypeScript compilation error. (Expects `input` to be typeof `string`)
narrowedRouter.query('invalidInputType', {
  input: z.number(),
  resolve: ({ input }) => {
    return { input };
  }
});

Additional changes

  • Added export of the Router type (returned by router()) as TRPCRouter.
     

@jlalmes
jlalmes requested review from KATT and sachinraja April 7, 2022 11:29
@vercel

vercel Bot commented Apr 7, 2022 •

Copy link
Copy Markdown

@jlalmes
jlalmes requested a review from LouisHaftmann April 7, 2022 11:32
LouisHaftmann
LouisHaftmann previously approved these changes Apr 7, 2022
@KATT

KATT commented Apr 7, 2022

Copy link
Copy Markdown
Member

Is this really required? It could just automatically omit the ones that don't fulfill the requirement for swagger?

@jlalmes

jlalmes commented Apr 7, 2022

Copy link
Copy Markdown
Contributor Author

I suppose it's not really required in the context of OpenAPI if we go with the approach shared here #1724 (comment). But this change doesn't affect current functionality nor introduces much of a TS overhead, and type narrowing is always good. Won't be offended if you want to close this one for now.

@jlalmes

jlalmes commented Apr 7, 2022 •

Copy link
Copy Markdown
Contributor Author

Regardless, would be great if we could export Router as TRPCRouter. (Currently Router is LegacyRouter)

@KATT

KATT commented Apr 7, 2022

Copy link
Copy Markdown
Member

Regardless, would be great if we could export Router as TRPCRouter. (Currently Router is LegacyRouter)

LegacyRouter is removed on the next-branch

@github-actions

github-actions Bot commented Oct 6, 2022

Copy link
Copy Markdown
Contributor

This pull request has been locked because it had no new activity for 30 days. If you think, this PR is still necessary, create a new one with the same branch. Thank you.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Oct 6, 2022
@KATT
KATT deleted the feature/input-type-narrowing branch November 22, 2022 09:48

This branch was successfully deployed

3 active deployments
Preview – next-prisma-starter — 03a2cbf3 Deployed Apr 7, 2022 by vercel[bot]
Preview – todomvc — 03a2cbf3 Deployed Apr 7, 2022 by vercel[bot]
Preview – www — 03a2cbf3 Deployed Apr 7, 2022 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants