export type NoInfer = [T][T extends any ? 0 : never] export type IsAny = 1 extends 0 & T ? Y : N export type IsAnyBoolean = 1 extends 0 & T ? true : false export type IsKnown = unknown extends T ? N : Y export type PickAsRequired = Omit & Required> export type PickAsPartial = Omit & Partial> export type PickUnsafe = K extends keyof T ? Pick : never export type PickExtra = Expand<{ [TKey in keyof K as string extends TKey ? never : TKey extends keyof T ? never : TKey]: K[TKey] }> export type PickRequired = { [K in keyof T as undefined extends T[K] ? never : K]: T[K] } export type Expand = T extends object ? T extends infer O ? { [K in keyof O]: O[K] } : never : T export type UnionToIntersection = ( U extends any ? (k: U) => void : never ) extends (k: infer I) => any ? I : never export type Values = O[ValueKeys] export type ValueKeys = Extract export type DeepAwaited = T extends Promise ? DeepAwaited : T extends Record> ? { [K in A]: DeepAwaited } : T export type PathParamMask = TRoutePath extends `${infer L}/$${infer C}/${infer R}` ? PathParamMask<`${L}/${string}/${R}`> : TRoutePath extends `${infer L}/$${infer C}` ? PathParamMask<`${L}/${string}`> : TRoutePath export type Timeout = ReturnType export type Updater = | TResult | ((prev?: TPrevious) => TResult) export type PickExtract = { [K in keyof T as T[K] extends U ? K : never]: T[K] } export type PickExclude = { [K in keyof T as T[K] extends U ? never : K]: T[K] } export function last(arr: T[]) { return arr[arr.length - 1] } export function warning(cond: any, message: string): cond is true { if (cond) { if (typeof console !== 'undefined') console.warn(message) try { throw new Error(message) } catch {} } return true } function isFunction(d: any): d is Function { return typeof d === 'function' } export function functionalUpdate( updater: Updater, previous: TResult, ) { if (isFunction(updater)) { return updater(previous as TResult) } return updater } export function pick(parent: T, keys: K[]): Pick { return keys.reduce((obj: any, key: K) => { obj[key] = parent[key] return obj }, {} as any) } /** * This function returns `a` if `b` is deeply equal. * If not, it will replace any deeply equal children of `b` with those of `a`. * This can be used for structural sharing between immutable JSON values for example. * Do not use this with signals */ export function replaceEqualDeep(prev: any, _next: T): T { if (prev === _next) { return prev } const next = _next as any const array = Array.isArray(prev) && Array.isArray(next) if (array || (isPlainObject(prev) && isPlainObject(next))) { const prevSize = array ? prev.length : Object.keys(prev).length const nextItems = array ? next : Object.keys(next) const nextSize = nextItems.length const copy: any = array ? [] : {} let equalItems = 0 for (let i = 0; i < nextSize; i++) { const key = array ? i : nextItems[i] copy[key] = replaceEqualDeep(prev[key], next[key]) if (copy[key] === prev[key]) { equalItems++ } } return prevSize === nextSize && equalItems === prevSize ? prev : copy } return next } // Copied from: https://github.com/jonschlinkert/is-plain-object export function isPlainObject(o: any) { if (!hasObjectPrototype(o)) { return false } // If has modified constructor const ctor = o.constructor if (typeof ctor === 'undefined') { return true } // If has modified prototype const prot = ctor.prototype if (!hasObjectPrototype(prot)) { return false } // If constructor does not have an Object-specific method if (!prot.hasOwnProperty('isPrototypeOf')) { return false } // Most likely a plain Object return true } function hasObjectPrototype(o: any) { return Object.prototype.toString.call(o) === '[object Object]' }