Currently, I am working on integrating chat functionality using the 'signal' events with the OpenTok API.
Here is my event listener that successfully receives the signal:
// Listen for signal CHAT_MESSAGE
sess.on('signal:CHAT_MESSAGE', event => {
const message = event.data
...
})
The issue arises when TypeScript does not recognize event.data
as a valid property. The type is defined in the Session class:
Session.signal: Event<'signal', Session> & {
type?: string;
data?: string;
from: Connection;
};
I attempted to specify the type by picking it from the Session class like this:
const message = event.data as Session['signal']
However, TypeScript displays an error stating
Property 'data' does not exist on type 'Event<string, any>'
. It seems that TS is struggling to correctly identify the event type...
As another attempt, I tried converting to 'unknown' first:
const signal = (event as unknown) as Session['signal']
const msg = signal.data
Now TypeScript gives the error:
Property 'data' does not exist on type '(signal: { type?: string | undefined; data?: string | undefined; to?: Connection | undefined; }, callback: (error?: OTError | undefined) => void) => void'.ts(2339)
The discrepancy between recognizing and not recognizing the 'data' property is puzzling.
I would appreciate any suggestions on how to resolve this issue without turning off TypeScript's type checking.