0
import { InboundResponseHeadersT } from './types'
export const handler = async () => {
  let status = true
  let lastId: string | null = null
  while (status) {
    try {
      //.       ERROR IS HERE
      let { data, messageData } = await axios
        .get('https://xxxxxxxx/httpadapter/inbound', {
          httpsAgent: new https.Agent({ rejectUnauthorized: false }),
          headers: {
            'X-DeleteAfterConfirmation': true,
            'X-LastId': lastId,
          },
          auth: {
            username: 'xxx',
            password: 'xxxx',
          },
        })
        .then((res) => ({
          data: res.data as string,
          messageData: res.headers as InboundResponseHeadersT,
        }))
      const parser = new XMLParser({ ignoreAttributes: false })
      let obj = parser.parse(data)

      await watchEvent(obj, messageData['x-conversationid'])
      lastId = messageData['x-id']
    } catch (err) {
      status = false
      break
    }
  }
}

The exporting type

export type InboundResponseHeadersT = {
  'x-messageversion': string
  'x-backendreceiverid': string
  'x-schemaset': string
  'x-conversationid': string
  'x-id': string
  'x-filename': string
  'x-messageid': string
  'content-type': 'text/xml'
  'x-backendsenderid': string
  'x-messagetype': string
  'x-content-type-options': string
  'x-xss-protection': string
  'strict-transport-security': string
  'x-frame-options': string
  'content-length': string
}

I have the following problem that my TS says that messageData has type any even if i set the type in the return statement. What am i doing wrong here?

enter image description here

EDIT:

After removing the while() loop the error dissapear. But i need the loop

bill.gates
  • 14,145
  • 3
  • 19
  • 47
  • Axios methods are generic, just pass the appropriate type parameters like `axios.get<{ data: string, messageData: InboundMessageHeaders }>()`. https://www.google.com/search?q=axios+typescript&oq=axios+types&aqs=chrome.0.0i20i131i263i433i512j69i57j0i512l8.2352j0j4&sourceid=chrome&ie=UTF-8. – Jared Smith Dec 13 '22 at 15:09
  • @JaredSmith as i edited, after removing `while` loop everything works fine. There is something going on with the while loop – bill.gates Dec 13 '22 at 15:16
  • That might be an interesting question in its own right (why is the compiler smart enough to infer the correct type in one case but not the other). For your actual (original) question though the duplicate suffices: just pass the generic type param. – Jared Smith Dec 13 '22 at 15:59

0 Answers0