I am getting a typescript error in my React Native project. I am not sure how to address this. Im new to Typescript. I have a function that is called throughout the app. I have to append headers to my api call. To keep code clean I add the headers in this function.
The error I get is with the axios function await axios(options)
:
other files in the project will call like this
useEffect(() => { const options = { method: 'get', url: `${network}/getMaps`, }; Api.apiRequest(options) .then(response => { setData(response.data.items); }) .catch(function (e) { console.log('another error happened fetching cameras:'+ e?.message); }) }, []);
Below is where I have the issueapiRequest.ts
// these interface types are in different files and I import them in. They are just here for now to simplify thingsinterface ApiRequest { method: 'get' | 'post'; url: string; } interface Options extends ApiRequest { headers: {'UUID': string | number[] | undefined;'ClientName': 'ELMC';'DeviceName': string;'ClientIcon': string; }; }const apiRequest = async (options:Options) =>{ const GUID = await getHttpRquestUUID(); const DeviceName = await getDeviceName(); // append headers to the request options options.headers = {'UUID': GUID,'ClientName': 'MyApp','DeviceName': getDeviceName,'ClientIcon': 'http:url.com' }; try { const results = await axios(options); } catch (error) { console.log(error) }}const Api = { apiRequest, };export {Api};
errorNo overload matches this call.Overload 1 of 2, '(config: AxiosRequestConfig): Promise<AxiosResponse<any, any>>', gave the following error.Argument of type 'APIOptions' is not assignable to parameter of type 'AxiosRequestConfig'.Types of property 'headers' are incompatible.Type '{ '-UUID': string | number[] | undefined; '-ClientName': "MyApp"; '-DeviceName': string; '-ClientIcon': string; }' is not assignable to type 'Partial<RawAxiosHeaders & MethodsHeaders & CommonHeaders>'.Property ''-UUID'' is incompatible with index signature.Type 'string | number[] | undefined' is not assignable to type 'AxiosHeaderValue | undefined'.