Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

vdom: Fix types to avoid relying on unsound bivariance.

Anders Kaseorge9ea4280ab90
diff --git a/web/src/pm_list_dom.ts b/web/src/pm_list_dom.ts
index 19d70d167b3..ed92bf778fb 100644
--- a/web/src/pm_list_dom.ts
+++ b/web/src/pm_list_dom.ts
@@ -10,40 +10,43 @@ type PMListConversation = {
user_ids_string: string;
};
-type PMNode = vdom.Node & {
- conversation: PMListConversation;
-};
+type PMNode =
+ | {
+ type: "conversation";
+ conversation: PMListConversation;
+ }
+ | {
+ type: "more_items";
+ more_conversations_unread_count: number;
+ };
-export function keyed_pm_li(conversation: PMListConversation): PMNode {
+export function keyed_pm_li(conversation: PMListConversation): vdom.Node<PMNode> {
const render = (): string => render_pm_list_item(conversation);
- const eq = (other: PMNode): boolean => _.isEqual(conversation, other.conversation);
+ const eq = (other: PMNode): boolean =>
+ other.type === "conversation" && _.isEqual(conversation, other.conversation);
const key = conversation.user_ids_string;
return {
key,
render,
- conversation,
eq,
+ type: "conversation",
+ conversation,
};
}
-type MorePrivateConversationsNode = vdom.Node & {
- more_items: boolean;
- more_conversations_unread_count: number;
-};
-
export function more_private_conversations_li(
more_conversations_unread_count: number,
-): MorePrivateConversationsNode {
+): vdom.Node<PMNode> {
const render = (): string =>
render_more_private_conversations({more_conversations_unread_count});
// Used in vdom.js to check if an element has changed and needs to
// be updated in the DOM.
- const eq = (other: MorePrivateConversationsNode): boolean =>
- other.more_items &&
+ const eq = (other: PMNode): boolean =>
+ other.type === "more_items" &&
more_conversations_unread_count === other.more_conversations_unread_count;
// This special key must be impossible as a user_ids_string.
@@ -51,14 +54,14 @@ export function more_private_conversations_li(
return {
key,
- more_items: true,
- more_conversations_unread_count,
render,
eq,
+ type: "more_items",
+ more_conversations_unread_count,
};
}
-export function pm_ul(nodes: vdom.Node[]): vdom.Tag {
+export function pm_ul(nodes: vdom.Node<PMNode>[]): vdom.Tag<PMNode> {
const attrs: [string, string][] = [
["class", "dm-list"],
["data-name", "private"],
diff --git a/web/src/vdom.ts b/web/src/vdom.ts
index 37d2e727c1e..412373b954e 100644
--- a/web/src/vdom.ts
+++ b/web/src/vdom.ts
@@ -2,22 +2,20 @@ import _ from "lodash";
import * as blueslip from "./blueslip";
-export type Node = {
+export type Node<T> = T & {
key: unknown;
render: () => string;
- // TODO: This is hiding type errors due to bivariance.
- // eslint-disable-next-line @typescript-eslint/method-signature-style
- eq(other: Node): boolean;
+ eq: (other: Node<T>) => boolean;
};
-type Options = {
+type Options<T> = {
attrs: [string, string][];
- keyed_nodes: Node[];
+ keyed_nodes: Node<T>[];
};
-export type Tag = {
+export type Tag<T> = {
tag_name: string;
- opts: Options;
+ opts: Options<T>;
};
export function eq_array<T>(
@@ -42,14 +40,14 @@ export function eq_array<T>(
return a.every((item, i) => eq(item, b[i]));
}
-export function ul(opts: Options): Tag {
+export function ul<T>(opts: Options<T>): Tag<T> {
return {
tag_name: "ul",
opts,
};
}
-export function render_tag(tag: Tag): string | undefined {
+export function render_tag<T>(tag: Tag<T>): string | undefined {
/*
This renders a tag into a string. It will
automatically escape attributes, but it's your
@@ -73,7 +71,7 @@ export function render_tag(tag: Tag): string | undefined {
return undefined;
}
- const innards = opts.keyed_nodes.map((node: Node) => node.render()).join("\n");
+ const innards = opts.keyed_nodes.map((node) => node.render()).join("\n");
return start_tag + "\n" + innards + "\n" + end_tag;
}
@@ -98,11 +96,11 @@ export function update_attrs(
}
}
-export function update(
+export function update<T>(
replace_content: (html: string | undefined) => void,
find: () => JQuery,
- new_dom: Tag,
- old_dom: Tag | undefined,
+ new_dom: Tag<T>,
+ old_dom: Tag<T> | undefined,
): void {
/*
The update method allows you to continually