import isPlainObject from 'lodash-es/isPlainObject'; import { InjectionKey, isRef, Ref } from 'vue'; /** * Create a union from an object's values * * using a plain object: * const SHAPE = { CIRCLE: 'circle', SQUARE: 'square' } as const; * ValueOf -> square | circle * * using a type: * type Shape = { SQUARE: string; CIRCLE: string; } * ValueOf */ export type ValueOf = T[keyof T]; export function isListOfStrings(list: unknown): list is string[] { return Array.isArray(list) && typeof list[0] === 'string'; } export function isListOfObjects(list: unknown): list is T[] { return Array.isArray(list) && isPlainObject(list[0]); } export interface Injection { key: InjectionKey; } export interface InjectionWithDefaults extends Injection { defaults: T; } /** * Like the MaybeRefOrGetter type from Vue, but without the Maybe. Can be used to ensure the most up-to-date value is available. */ export type RefOrGetter = Ref | (() => T); /** * A type guard for the RefOrGetter type. Acceptable values include: * - a value created from `ref()` * - a value created from `shallowRef()` * - a value created from `computed()` * - a function that returns a value */ export function isRefOrGetter(value: RefOrGetter | unknown): value is RefOrGetter { return isRef(value) || typeof value === 'function'; }