I'm trying to define an object model that contains an object. This object can be of two diferent types of object.
export interface EventParams {
evtType: string;
evtData: FaultOrGoalData| SwapData;
}
export interface FaultOrGoalData {
evtName: string;
player: string;
position: string;
}
export interface SwapData {
swapPlayer: string;
}
My problem here is ts lint telling me that it's impossible to access the data contained in an encapsulated object.
Example: params.evtData.evtName
Hence my question: is it possible to create a union type with interfaces?
params.evtData.evtNamewithout checking ifparams.evtDatais aFaultOrGoalDatafirst, that's the sort of error TypeScript is designed to catch. If you are checking, and TypeScript is still reporting an error, then it might not be one of the checks the compiler understands. As it stands, the answer to your question is: "yes it is possible, and the error you're getting is a good one".params.evtDatais going to have anevtNamemember, so I'm complaining about it now." @TitianCernicova-Dragomir's answer is one way to deal with the error.