Typescript expressjwt和expressgraphql:error TS2339:Property';用户';不存在于类型';请求';
我试图让ExpressJWT和graphql在typescript中协同工作Typescript expressjwt和expressgraphql:error TS2339:Property';用户';不存在于类型';请求';,typescript,express-jwt,express-graphql,Typescript,Express Jwt,Express Graphql,我试图让ExpressJWT和graphql在typescript中协同工作 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 a
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
}
}))
)
我导入了相关的打字@types/express
,@types/express graphql
和@types/express jwt
有一个typescript错误:
error TS2339: Property 'user' does not exist on type 'Request'
express jwt
将user
添加到请求对象上
如何解决此问题?使用一个属性扩展Express
请求
interface AuthRequest extends express.Request {
user?: string
}
app.use(
'/',
expressGraphql((req: AuthRequest, res, graphQLParams) => ({
schema,
rootValue,
context: {
user: req.user
}
}))
)