Take, for example, an interface or type alias describing a websocket message:
interface WSMessage {
type: 'msg_x' | 'msg_y' | 'msg_z'
x_prop?: string;
y_prop?: string;
z_prop?: string;
}
Can you dynamically cast something like const data = JSON.parse(event.data) as WSMessage;
based on the value of its type property? And thus ensure whether you're dealing with a WSMessage that has the matching property?
Assume I've got an interface that describes each variety with type containing a single possibility and only the appropriate property as well.
Thank you for your time!