express-jwt and express-graphql: error TS2339: Property 'user' does not exist on type 'Request'
See original GitHub issueI try to make express-jwt and graphql work together in typescript.
I asked about it on stackoverflow but had no answer. So I repost here.
import * as express from 'express'
import * as expressGraphql from 'express-graphql'
import * as expressJwt from 'express-jwt'
import schema from './api/schemas'
import rootValue from './api/resolvers'
const app = express()
app.use(
expressJwt({
credentialsRequired: false,
secret: process.env.JWT_SECRET
})
)
app.use(
'/',
expressGraphql((req, res, graphQLParams) => ({
schema,
rootValue,
context: {
user: req.user
}
}))
)
I imported the relative typings @types/express
, @types/express-graphql
and @types/express-jwt
.
There is a typescript error:
error TS2339: Property 'user' does not exist on type 'Request'
user
is added on the request object by express-jwt
.
How can I fix that?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:8
- Comments:11 (2 by maintainers)
Top Results From Across the Web
error TS2339: Property 'user' does not exist on type 'Request ...
I imported the relative typings @types/express , @types/express-graphql and @types/express-jwt . There is a typescript error: error TS2339: ...
Read more >Property does not exist on type Request in TypeScript
The "Property does not exist on type Request" error occurs when we access a property that does not exist in the Request interface....
Read more >type-graphql/Lobby - Gitter
src/index.ts:45:27 - error TS2339: Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'. 45 user: req.user ...
Read more >How To Build GraphQL APIs with ExpressJS and Typescript
GraphQL is a query language for APIs. In this article, we will learn how to build a GraphQL API with Express.js and MongoDB....
Read more >Unable to compile TypeScript. Property 'user' does not exist on ...
At first typescript has not accepted the req.user. I found a hint on StackOverflow and brought in a @types folder with a file...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thank to declaration merging, you can add the user property directly to express’s request model like so:
Declaring the token interface as global allows you to add additional properties to it as well:
You’ll need to make sure to reference the typescript file that you put these declarations in though.
I’m fairly new to TypeScript too, that’s why I didn’t post the code. Your code is similar to mine, but we were using it for different things (I wasn’t passing it through GraphQL). For your use case, that interface should work. 😃