Currently, I am dealing with two types of data: GenArtWorkerMsg
and VehicleWorkerMsg
. Despite having a unique type
property on the payload, my Searcher is unable to differentiate between these data-sets when passed in.
How can I make it understand and distinguish between the different payloads?
Check out my solution on Typescript Playground so far.
type GenArtWorkerMsg = {
extra: 'names' | 'descs';
data: LoadTableReturn['2'];
type: 'vehicleGroups';
};
type VehicleWorkerMsg = {
extra: 'carName' | 'carGroup';
data: LoadTableReturn['3'];
type: 'genericArticles';
};
type SearchActions = {
genericArticles: GenArtWorkerMsg;
vehicleGroups: VehicleWorkerMsg;
};
type SearchType<type extends keyof SearchActions> = {
term_id: number;
lang_ids: number[];
dataOwner: SearchActions[type]['data'][number];
extra: SearchActions[type]['extra'];
type: type;
uuid: string;
translations?: translations[];
};
type TransAction = {
type: 'completedSearch';
payload: SearchType<'genericArticles' | 'vehicleGroups'>;
}
const messageHandler = (action: TransAction) => {
const { uuid, dataOwner, translations, extra, type } = action.payload;
switch (action.payload.type) {
case 'genericArticles':
{
// Only GenArtWorkerMsg data has type 'genericArticles', but the payload includes both GenArtWorkerMsg and VehicleWorkerMsg