The ResponseMessageHandler is a TypeScript utility type that describes the shape of a handler function used with addZpaxMessageListener. It ensures that the handler receives the correct message type and corresponding data.
Definition
export type ZpaxMessage<T, D> = {
type: T;
data: D;
};
export type ResponseMessageHandler<T extends keyof ResponseMessage> = (
data: ZpaxMessage<T, ResponseMessage[T]>
) => void;Explanation
-
ZpaxMessage<T, D>Represents a message object with atypeanddatafield. -
ResponseMessageHandler<T>A function that takes aZpaxMessagewhere:-
Tis a key of theResponseMessageinterface, representing a specific message type. -
datais typed to match the corresponding value inResponseMessage[T].
-
Example Usage
addZpaxMessageListener('last_interaction', (message) => {
const timestamp = message.data;
console.log('Last interaction time:', timestamp);
});Here:
'last_interaction' is a key of ResponseMessage.
message.data will be typed as ResponseMessage['last_interaction'].