/** * @module * Types utility. */ /* eslint-disable @typescript-eslint/no-explicit-any */ export type Expect = T export type Equal = (() => T extends X ? 1 : 2) extends () => T extends Y ? 1 : 2 ? true : false export type NotEqual = true extends Equal ? false : true export type UnionToIntersection = (U extends any ? (k: U) => void : never) extends ( k: infer I ) => void ? I : never export type RemoveBlankRecord = T extends Record ? (K extends string ? T : never) : never export type IfAnyThenEmptyObject = 0 extends 1 & T ? {} : T export type JSONPrimitive = string | boolean | number | null export type JSONArray = (JSONPrimitive | JSONObject | JSONArray)[] export type JSONObject = { [key: string]: JSONPrimitive | JSONArray | JSONObject | object | InvalidJSONValue } export type InvalidJSONValue = undefined | symbol | ((...args: unknown[]) => unknown) type InvalidToNull = T extends InvalidJSONValue ? null : T type IsInvalid = T extends InvalidJSONValue ? true : false /** * symbol keys are omitted through `JSON.stringify` */ type OmitSymbolKeys = { [K in keyof T as K extends symbol ? never : K]: T[K] } export type JSONValue = JSONObject | JSONArray | JSONPrimitive /** * Convert a type to a JSON-compatible type. * * Non-JSON values such as `Date` implement `.toJSON()`, * so they can be transformed to a value assignable to `JSONObject` * * `JSON.stringify()` throws a `TypeError` when it encounters a `bigint` value, * unless a custom `replacer` function or `.toJSON()` method is provided. * * This behaviour can be controlled by the `TError` generic type parameter, * which defaults to `bigint | ReadonlyArray`. * You can set it to `never` to disable this check. */ export type JSONParsed> = T extends { toJSON(): infer J } ? (() => J) extends () => JSONPrimitive ? J : (() => J) extends () => { toJSON(): unknown } ? {} : JSONParsed : T extends JSONPrimitive ? T : T extends InvalidJSONValue ? never : T extends ReadonlyArray ? { [K in keyof T]: JSONParsed, TError> } : T extends Set | Map | Record ? {} : T extends object ? T[keyof T] extends TError ? never : { [K in keyof OmitSymbolKeys as IsInvalid extends true ? never : K]: boolean extends IsInvalid ? JSONParsed | undefined : JSONParsed } : T extends unknown ? T extends TError ? never : JSONValue : never /** * Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability. * @copyright from sindresorhus/type-fest */ export type Simplify = { [KeyType in keyof T]: T[KeyType] } & {} /** * A simple extension of Simplify that will deeply traverse array elements. */ export type SimplifyDeepArray = T extends any[] ? { [E in keyof T]: SimplifyDeepArray } : Simplify export type InterfaceToType = T extends Function ? T : { [K in keyof T]: InterfaceToType } export type RequiredKeysOf = Exclude< { [Key in keyof BaseType]: BaseType extends Record ? Key : never }[keyof BaseType], undefined > export type HasRequiredKeys = RequiredKeysOf extends never ? false : true export type IsAny = boolean extends (T extends never ? true : false) ? true : false /** * String literal types with auto-completion * @see https://github.com/Microsoft/TypeScript/issues/29729 */ export type StringLiteralUnion = T | (string & Record)