[SOLVED] Property 'authorization' does not exist on type 'Request'


Consider this code:

  setContext(async (req, { headers }) => {
    const token = await getToken(config.resources.gatewayApi.scopes)

    const completeHeader = {
      headers: {
          token && token.accessToken ? `Bearer ${token.accessToken}` : '',
      } as Express.Request,

    console.log('accessToken: ', completeHeader.headers.authorization)
    return completeHeader

Which generates the following TS error:

Property ‘authorization’ does not exist on type ‘Request’.

This comes from trying to access completeHeader.headers.authorization. The property authorization is indeed not available on the Express.request interface. It’s strange that TypeScript can’t infere the type from the literal object, which clearly is of type string. When not defining the type as Express.Request an error is thrown about an unsafe any assignment.

Is it required to create a new TS interface just for this one field? Or are we using an incorrect type? The field authorization looks to be like a commonly used field for sending tokens.


The reason is because you’re coercing completeHeader.headers into the Express.Request type. The coerced type overrides the inferred type.

What you can do, is expand that coerced type by doing the following:

as Express.Request & { authorization: string }

or you could create an entirely new type:

type AuthorizedRequest = Express.Request & { authorization: string };
as AuthorizedRequest 

Answered By – Dane Brouwer

Answer Checked By – David Goodson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *