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.

express-jwt and express-graphql: error TS2339: Property 'user' does not exist on type 'Request'

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Reactions:8
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
Beng89commented, Dec 13, 2019

Thank to declaration merging, you can add the user property directly to express’s request model like so:

declare global {
  interface ParsedToken {
    iss: string
    sub: string
    aud: string | string[]
    iat: number
    exp: number
    azp: string
    scope: string
  }

  namespace Express {
    interface Request {
      user?: ParsedToken
    }
  }
}

Declaring the token interface as global allows you to add additional properties to it as well:

declare global {
  interface ParsedToken {
    additionalProp: string
  }
}

You’ll need to make sure to reference the typescript file that you put these declarations in though.

1reaction
jellzcommented, Feb 14, 2019

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. 😃

Read more comments on GitHub >

github_iconTop 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 >

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