So running into another vague Typescript error.
I have the following function fetchAllAssets
which ends with dispatching an action and running the responses
through a function called formatAssets
.
And the error is on the responses
array.
export const fetchAllAssets = () => (dispatch: DispatchAllAssets) => {
dispatch(actionGetAllAssets());
return fetchAll([getPrices(), getAvailableSupply()]).then((responses) =>
dispatch(actionSetAllAssets(formatAssets(responses))));
}
The formatAssets function:
export const formatAssets = (responses: IResponseConfig[]) => {
let prices: any;
let availableSupplies: any;
console.log('responses', responses);
responses.forEach((response: IResponseConfig) => {
const { config } = response;
const { url } = config;
if (url.includes('prices')) {
prices = response.data;
} else if (url.includes('dashboard')) {
availableSupplies = cleanAssets(response.data);
}
return {
prices,
availableSupplies
};
});
The Error
Argument of type '[{}, {}, {}, {}, {}, {}, {}, {}, {}, {}]' is not assignable to parameter of type 'IResponseConfig[]'. Type '{}' is missing the following properties from type 'IResponseConfig': config, data ts(2345)
Here is what the interface for IResponseConfig looks like:
export interface IResponseConfig {
config: {
adapter: any;
baseUrl: string;
headers: {};
maxContentLength: number;
method: string;
params: {
key: string;
}
timeout: number;
transformRequest: {};
transformResponse: {};
url: string;
validateStatus: any;
xsrfCookieName: string;
xsrfHeaderName: string;
};
data: IAssetResponse[];
headers?: {};
request?: XMLHttpRequest;
upload?: XMLHttpRequestUpload;
status?: number;
statusText?: string;
}
And here is what responses is:
What shape should IResponseConfig take in order for the error to go away?
Signature of fetchAll
// @TODO Fix any type.
export const fetchAll = (array: any) => Promise.all(array);
Also I was able to remove the error by making all keys optional (hack?)
export interface IResponseConfig {
config?: any;
data?: IAssetResponse[];
headers?: any;
request?: XMLHttpRequest;
upload?: XMLHttpRequestUpload;
status?: number;
statusText?: string;
}
What IAssetResponse
looks like (the actual data I care about)
export interface IAssetResponse {
[key: string]: string | undefined;
currency: string;
price?: string;
availableSupply?: string;
maxSupply?: string;
}