我尝试让express-jwt和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 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。
存在打字错误:
error TS2339: Property 'user' does not exist on type 'Request'express-jwt在request对象上添加了user。
我怎么才能修复它呢?
发布于 2019-03-02 06:37:07
使用一个属性扩展Express Request
interface AuthRequest extends express.Request {
user?: string
}
app.use(
'/',
expressGraphql((req: AuthRequest, res, graphQLParams) => ({
schema,
rootValue,
context: {
user: req.user
}
}))
)https://stackoverflow.com/questions/54462222
复制相似问题