From 819bccfec16ea4efce99a682a66ef1421d43b6f2 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Fri, 3 May 2024 18:25:26 -0700 Subject: [PATCH] submessage: Fix implicit use of any. Signed-off-by: Anders Kaseorg --- web/shared/src/poll_data.ts | 15 ++++++++------- web/src/submessage.ts | 4 ++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/web/shared/src/poll_data.ts b/web/shared/src/poll_data.ts index 6133bce215..37dbb1a62a 100644 --- a/web/shared/src/poll_data.ts +++ b/web/shared/src/poll_data.ts @@ -30,16 +30,11 @@ export type WidgetData = { question: string; }; -export type InboundData = Record & {type: string}; +export type InboundData = unknown; export type NewOptionOutboundData = {type: string; idx: number; option: string}; export type QuestionOutboundData = {type: string; question: string}; export type VoteOutboundData = {type: string; key: string; vote: number}; export type PollHandle = { - // Add generic key property to allow string indexing on PollHandle type in `handle_event` method. - [key: string]: { - outbound: (arg: string) => InboundData | undefined; - inbound: (sender_id: number, data: InboundData) => void; - }; new_option: { outbound: (option: string) => NewOptionOutboundData; inbound: (sender_id: number | string, data: InboundData) => void; @@ -294,8 +289,14 @@ export class PollData { } handle_event(sender_id: number, data: InboundData): void { + assert( + typeof data === "object" && + data !== null && + "type" in data && + typeof data.type === "string", + ); const type = data.type; - if (this.handle[type]) { + if (type === "new_option" || type === "question" || type === "vote") { this.handle[type].inbound(sender_id, data); } else { this.report_error_function(`poll widget: unknown inbound type: ${type}`); diff --git a/web/src/submessage.ts b/web/src/submessage.ts index a27c3fea33..80dc092309 100644 --- a/web/src/submessage.ts +++ b/web/src/submessage.ts @@ -79,7 +79,7 @@ export function get_message_events(message: Message): SubmessageEvents | undefin message.submessages.sort((m1, m2) => m1.id - m2.id); - const events = message.submessages.map((obj) => ({ + const events = message.submessages.map((obj): {sender_id: number; data: unknown} => ({ sender_id: obj.sender_id, data: JSON.parse(obj.content), })); @@ -186,7 +186,7 @@ export function handle_event(submsg: Submessage): void { return; } - let data; + let data: unknown; try { data = JSON.parse(submsg.content);