From 5c44cb5cc66472400a7e1b28f0d3f341b99642dd Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 17 Jan 2022 14:08:36 +0000 Subject: [PATCH] Improve QueryMatcher TypeScript definition (#7555) --- src/@types/common.ts | 11 +++++++++++ src/autocomplete/QueryMatcher.ts | 5 +++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/@types/common.ts b/src/@types/common.ts index 36ef7a9ace..991cfb7e08 100644 --- a/src/@types/common.ts +++ b/src/@types/common.ts @@ -23,3 +23,14 @@ export type Writeable = { -readonly [P in keyof T]: T[P] }; export type ComponentClass = keyof JSX.IntrinsicElements | JSXElementConstructor; export type ReactAnyComponent = React.Component | React.ExoticComponent; + +// Based on https://stackoverflow.com/a/58436959 +type Join = K extends string | number ? + P extends string | number ? + `${K}${"" extends P ? "" : "."}${P}` + : never : never; + +type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...0[]]; + +export type Leaves = [D] extends [never] ? never : T extends object ? + { [K in keyof T]-?: Join> }[keyof T] : ""; diff --git a/src/autocomplete/QueryMatcher.ts b/src/autocomplete/QueryMatcher.ts index 737b0d80ce..e8746c12d8 100644 --- a/src/autocomplete/QueryMatcher.ts +++ b/src/autocomplete/QueryMatcher.ts @@ -20,10 +20,11 @@ import { at, uniq } from 'lodash'; import { removeHiddenChars } from "matrix-js-sdk/src/utils"; import { TimelineRenderingType } from '../contexts/RoomContext'; +import { Leaves } from "../@types/common"; interface IOptions { - keys: Array; - funcs?: Array<(T) => string | string[]>; + keys: Array>; + funcs?: Array<(o: T) => string | string[]>; shouldMatchWordsOnly?: boolean; // whether to apply unhomoglyph and strip diacritics to fuzz up the search. Defaults to true fuzzy?: boolean;