/** * @description Define a conditional usage function for a target object. */ export type Use = (use: (target: Target) => R, otherwise: () => R) => R /** * @description Create a conditional usage helper from getter and availability checks. */ export const useFactory = (get: () => Target, has: () => boolean): Use => { const use = (use: (target: Target) => R, otherwise: () => R): R => { if (has() === true) { const target = get() return use(target) } else { return otherwise() } } return use }