export const brandSymbol = Symbol('brand'); /** * @typeParam BrandStr - String type to brand this object with * @returns A branded empty object type * * @example * Brand<'example'> => {[brandSymbol]: 'example'} * * Which I might use like this: * type MyType = {content: string} & Brand<'example'> */ export type Brand = { [brandSymbol]: BrandStr; }; /** * Create an object of a specific type Brand * string branded type. * * @param brand: The string to Brand onto a simple object * @returns A branded empty object * * @example * brand('example') => {[brandSymbol]: 'example'} * * Which I might use like this: * const myType = {content: "default content", ...brand<'example'>} */ export function brand( brand: BrandStr, ): Brand { return { [brandSymbol]: brand, }; } /** * * @param branded: Branded object * @returns The string brand value */ export function getBrand(branded: Brand) { return branded[brandSymbol]; }