Typescript compiler keeps throwing error when I try to use union or multiple types / interfaces.
My requirement
From the server I receive an object as response in which one key ('errorMessages') has data type string[] and the other key ('data') can either be an object or an array. I wrote an interface for this which should look like below:
interface MyServerResponse {
errorMessages: string[];
data: {
headers: string[];
body: any[][];
} | any[]>;
}
Received compiler error on trying to access 'headers'
Property 'headers' does not exist on type 'any[] | { headers: string[]; body: any[][]; }'
Isn't it possible to implement this using union just like for number | boolean, string | null, etc.?
any[]be on body ?interface MyServerResponse { errorMessages: string[]; data: { headers: string[]; body: any[][]| any[]; } ; }bodykey within data is alwaysany[][]. I want the union on data key (just as mentioned in the question). Updated the question for more clarity.