Here is a code snippet to consider:
setContext(async (req, { headers }) => {
const token = await getToken(config.resources.gatewayApi.scopes)
const completeHeader = {
headers: {
...headers,
authorization:
token && token.accessToken ? `Bearer ${token.accessToken}` : '',
} as Express.Request,
}
console.log('accessToken: ', completeHeader.headers.authorization)
return completeHeader
})
An issue arises with the following TS error message:
Property 'authorization' does not exist on type 'Request'.
The error occurs when trying to retrieve
completeHeader.headers.authorization
. This property authorization
is not part of the Express.request
interface. It seems odd that TypeScript cannot infer the correct type from the literal object, which is clearly a string
. Without explicitly setting the type to as Express.Request
, an unsafe any assignment error is thrown.
Do we need to create a new TS interface just for this single field? Or are we utilizing an incorrect type definition? The authorization
field appears to be commonly used for transmitting tokens.