import { type Kind, type IsEqual, type Or, type GetKindValue } from "../common"; import { type Entity } from "./entity"; declare const flagHandlerKind: import("../common").KindHandler>; export declare const flagKind: import("../common").KindHandler>>; export interface FlagHandler = never> extends Kind { /** * The flag name stored as the key on the entity. * */ readonly name: GenericName; /** * Adds the flag to an entity and returns the enriched entity. * * ```ts * export function isMajor(entity: Entity) { * if (C.greaterThan(entity.age, 18)) { * return E.success( * MajorFlag.append(entity, { age: entity.age }), * ); * ``` * */ append(...args: IsEqual extends true ? [] : [GenericInputValue]): (entity: GenericInputEntity) => (GenericInputEntity & Flag); append(entity: GenericInputEntity, ...args: IsEqual extends true ? [] : [GenericInputValue]): (GenericInputEntity & Flag); /** * Retrieves the value carried by the flag. * * ```ts * const flagged = User.MajorFlag.append(user, { age: user.age }); * const value = User.MajorFlag.getValue(flagged); * ``` * */ getValue>(entity: GenericInputEntity): GetKindValue[GenericName]; /** * Checks whether the entity has this state and narrows its type to include the flag. * * ```ts * User.MajorFlag.has(user); // false * User.MajorFlag.has(flagged); // true * ``` * */ has(entity: GenericInputEntity): entity is Extract>; } export interface Flag extends Kind> { } /** * Creates a handler for assigning a typed business state to an entity. * * **Supported call styles:** * - Classic: `createFlag(name)` -> returns a handler * * Use flags to make an entity state part of a precise type contract. Functions can require an entity combined with one or more flags, while the same entity keeps its original business properties and can reuse its repositories, mappers, and other supporting code. A flag can optionally carry data associated with its state. * * ```ts * namespace User { * export const Name = C.createNewType("UserName", DP.string()); * export type Name = C.GetNewType; * * export const Age = C.createNewType("UserAge", DP.number(), C.Positive); * export type Age = C.GetNewType; * * export const Entity = C.createEntity("UserEntity", () => ({ * name: Name, * age: Age, * })); * export type Entity = C.GetEntity; * * export const MajorFlag = C.createFlag< * Entity, // mandatory * "majorUser", // mandatory * { age: Age } // optional * >("majorUser"); * export type MajorFlag = C.GetFlag; * * export function isMajor(entity: Entity) { * if (C.greaterThan(entity.age, 18)) { * return E.success( * MajorFlag.append(entity, { age: entity.age }), * ); * } * return E.left("not-major"); * } * } * * function drinkAlcohol(userEntity: User.Entity & User.MajorFlag) { * // ... * return E.right("not-thirsty-anymore"); * } * * const user = User.Entity.new({ * name: User.Name.createOrThrow("Julie"), * age: User.Age.createOrThrow(35), * }); * * const result = pipe( * user, * User.isMajor, * E.whenIsRight( * drinkAlcohol, * ), * ); * // E.Left<"not-major", undefined> | E.Right<"not-thirsty-anymore", undefined> * * const flagged = User.MajorFlag.append(user, { age: user.age }); * const value = User.MajorFlag.getValue(flagged); * * User.MajorFlag.has(user); // false * User.MajorFlag.has(flagged); // true * ``` * * @remarks * A flag avoids defining and maintaining a separate entity variation for every possible state. `append` enriches the entity's type with that state, `has` checks and narrows it at runtime, and `getValue` retrieves the data carried by the state. * * @see https://utils.duplojs.dev/en/v1/api/clean/flag * * @namespace C * */ export declare function createFlag = never>(name: Or<[ IsEqual, IsEqual ]> extends true ? never : NoInfer): FlagHandler; export declare namespace createFlag { var overrideHandler: import("../common").OverrideHandler, string, never>>; } export type GetFlag> = Extract>, any>; export {};