Hello. Help solve the error in TypeScript code. There is a function that makes a request to the server:
http.get(url) .then((API: IResponse) => { if (API.data.response) { console.log(API.data.response); } else if (API.data.error) { console.log(API.data.error); } }); The server always returns two kinds of objects.
In case of error:
{ data: { error: { error_code: number error_msg: string } } } And if successful:
{ data: { response: {} } } The response object can store any fields and values (Depends on the request). I described the interface like this:
interface IResponse { data: IResponseSuccess | IResponseError; } interface IResponseSuccess { response: {}; } interface IResponseError { error: { error_code: number | string; error_msg: string; } } Unfortunately, the TypeScript compiler produces errors:
Property 'response' does not exist on type 'IResponseSuccess | IResponseError'.Property 'error' does not exist on type 'IResponseSuccess | IResponseError'.