/* Copyright 2020 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import { JSXElementConstructor } from "react"; // Based on https://stackoverflow.com/a/53229857/3532235 export type Without = { [P in Exclude]?: never }; export type XOR = T | U extends object ? (Without & U) | (Without & T) : T | U; export type Writeable = { -readonly [P in keyof T]: T[P] }; export type ComponentClass = keyof JSX.IntrinsicElements | JSXElementConstructor; /** * Utility type for string dot notation for accessing nested object properties. * Based on https://stackoverflow.com/a/58436959 * @example * { * "a": { * "b": { * "c": "value" * }, * "d": "foobar" * } * } * will yield a type of `"a.b.c" | "a.d"` with Separator="." * @typeParam Target the target type to generate leaf keys for * @typeParam Separator the separator to use between key segments when accessing nested objects * @typeParam LeafType the type which leaves of this object extend, used to determine when to stop recursion * @typeParam MaxDepth the maximum depth to recurse to * @returns a union type representing all dot (Separator) string notation keys which can access a Leaf (of LeafType) */ export type Leaves = [ MaxDepth, ] extends [never] ? never : Target extends LeafType ? "" : { [K in keyof Target]-?: Join, Separator>; }[keyof Target]; type Prev = [never, 0, 1, 2, 3, ...0[]]; type Join = K extends string | number ? P extends string | number ? `${K}${"" extends P ? "" : S}${P}` : never : never; export type RecursivePartial = { [P in keyof T]?: T[P] extends (infer U)[] ? RecursivePartial[] : T[P] extends object ? RecursivePartial : T[P]; }; export type KeysStartingWith = { // eslint-disable-next-line @typescript-eslint/no-unused-vars [P in keyof Input]: P extends `${Str}${infer _X}` ? P : never; // we don't use _X }[keyof Input]; export type NonEmptyArray = [T, ...T[]]; export type Defaultize = P extends any ? string extends keyof P ? P : Pick> & Partial>> & Partial>> : never; export type DeepReadonly = T extends (infer R)[] ? DeepReadonlyArray : T extends Function ? T : T extends object ? DeepReadonlyObject : T; interface DeepReadonlyArray extends ReadonlyArray> {} type DeepReadonlyObject = { readonly [P in keyof T]: DeepReadonly; }; export type AtLeastOne }> = Partial & U[keyof U];