question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[typescript] Add the possibility to extend `NextApiRequest` with custom fields

See original GitHub issue

Feature request

It’s often the case, when we add a middleware for example, that we need to add extra informations in the req object.

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
  req.session.set('user', user)
}

This emit a TS error:

Property 'session' does not exist on type 'NextApiRequest'.

I would like a way to extend the type NextApiRequest with session here for example.

Express tackles this problem with declaration merging: see here.

As a temporary workaround, I created a custom type, but that’s not ideal because it forces me to import this type instead of the “builtin” type next provides.

export type NextApiRequestWithSession = NextApiRequest & {
  session: Session
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Kerumencommented, Apr 25, 2020

Nice! Thanks 😃

Looking forward for #12186

0reactions
balazsorban44commented, Jan 30, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add TypeScript types to request body in Next.js API ...
You can create a new interface that extends NextApiRequest and adds the typings for the two fields. interface ExtendedNextApiRequest extends ...
Read more >
How to extend the Express Request object in TypeScript
Let's learn how to extend the Request type in TypeScript to make its instances store custom data you can use at the controller...
Read more >
Next.js + TypeScript: Tips and FAQs for Every Stage of Your ...
While next.config.js must be a JavaScript file since it does not get parsed by Babel or TypeScript, it is possible to add some...
Read more >
Basic Features: TypeScript - Next.js
Next.js supports TypeScript by default and has built-in types for pages ... Providing your own tsconfig.json with custom compiler options is also supported....
Read more >
How to Unit Test Next.js API Routes with TypeScript
js framework because it offered so many of the benefits I listed above, one of the most important being the the ability to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found