global: Specialize jQuery .val() for elements with known value types.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2023-09-22 14:25:08 -07:00 committed by Tim Abbott
parent 06e8d1fb16
commit eefd7feafa
2 changed files with 16 additions and 4 deletions

16
web/src/global.d.ts vendored
View File

@ -22,7 +22,21 @@ declare namespace JQueryValidation {
}
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface JQuery {
interface JQuery<TElement = HTMLElement> {
// Specialize .val() for elements with known value types.
// https://github.com/DefinitelyTyped/DefinitelyTyped/pull/66801
val():
| (TElement extends HTMLSelectElement & {type: "select-one"}
? string
: TElement extends HTMLSelectElement & {type: "select-multiple"}
? string[]
: TElement extends HTMLSelectElement
? string | string[]
: TElement extends {value: string | number}
? TElement["value"]
: string | number | string[])
| undefined;
expectOne(): this;
get_offset_to_window(): DOMRect;
tab(action?: string): this; // From web/third/bootstrap

View File

@ -1,5 +1,4 @@
import _ from "lodash";
import assert from "minimalistic-assert";
// How to determine the direction of a paragraph (P1-P3): https://www.unicode.org/reports/tr9/tr9-35.html#The_Paragraph_Level
// Embedding level: https://www.unicode.org/reports/tr9/tr9-35.html#BD2
@ -142,8 +141,7 @@ export function get_direction(str: string): "ltr" | "rtl" {
export function set_rtl_class_for_textarea($textarea: JQuery<HTMLTextAreaElement>): void {
// Set the rtl class if the text has an rtl direction, remove it otherwise
let text = $textarea.val();
assert(typeof text === "string", "Passed HTML element must be a textarea.");
let text = $textarea.val()!;
if (text.startsWith("```quote")) {
text = text.slice(8);
}