5

I am new in Typescript. I am converting my code from JavaScript to Typescript. My code below will work in JavaScript, but it has an error in Typescript

import axios from "axios";

const data = JSON.stringify([eventID]);

const config = {
    method: "delete",
    url: url,
    headers: { 
        "Authorization": `Bearer ${elasticPrivateKey}`, 
        "Content-Type": "application/json",
    },
    data: data,
};

return axios(config);

I have an error like this

enter image description here

I don't understand how to get and configure this AxiosRequestConfig

sarah
  • 3,819
  • 4
  • 38
  • 80
  • 1
    You can inline the config definition into the axios call (`axios({ method: "delete", ...}`) or use `as const` to make the type checking more specific - the inferred type of `method: string` would allow plenty of values that *aren't* valid methods. Here is my answer to a similar question: https://stackoverflow.com/a/62652353/3001761 – jonrsharpe Feb 05 '21 at 12:02
  • Or you can define `const config: AxiosRequestConfig = {` – Anatoly Feb 05 '21 at 12:07

2 Answers2

8

I am not sure but try this

import axios,{ AxiosRequestConfig } from 'axios';

const config:AxiosRequestConfig = {
        method: "delete",
        url: url,
        headers: { 
            "Authorization": `Bearer ${elasticPrivateKey}`, 
            "Content-Type": "application/json",
        },
        data: data,
    };
2

You can do this:

import axios, { AxiosRequestConfig } from 'axios'

const config: AxiosRequestConfig = {
  baseURL: 'https://api.pagar.me/core/v5',
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json',
    Authorization: `${process.env.BASIC}`
  }
}

const pagarmeApi = axios.create(config)

export default pagarmeApi