import { type ForbiddenString } from "../string"; import { type Or, type IsEqual, type BreakGenericLink, type Adaptor, type UnionToIntersection, type AnyConstructor, type And, type SimplifyTopLevel } from "./types"; import { type GetPropsWithValue, type PartialKeys } from "../object"; export interface KindHandler { definition: GenericKindDefinition; runTimeKey: string; has(input: GenericInput): input is Extract>; addTo(...args: Or<[ IsEqual, IsEqual, IsEqual ]> extends true ? [input: GenericInput, value?: GenericValue] : [input: GenericInput, value: GenericValue]): Kind & BreakGenericLink; /** * @deprecated This method make mutation. */ setTo(...args: Or<[ IsEqual, IsEqual, IsEqual ]> extends true ? [input: GenericInput, value?: GenericValue] : [input: GenericInput, value: GenericValue]): Kind & BreakGenericLink; getValue>(input: GenericKind): GenericKind[SymbolKind][GenericKindDefinition["name"]]; } export interface KindDefinition { name: GenericName; value: GenericValue; } declare const SymbolKind: unique symbol; export type SymbolKind = typeof SymbolKind; export interface Kind { [SymbolKind]: { [Prop in GenericKindDefinition["name"]]: GenericValue; }; } export type RemoveKind> = Omit; export type GetKindValue> = GenericObject[SymbolKind][GenericKindHandler["definition"]["name"]]; export type GetKindHandler> = { [Prop in keyof GenericObject[SymbolKind]]: KindHandler, GenericObject[SymbolKind][Prop]>>; }[keyof GenericObject[SymbolKind]]; export interface GetKind> { [SymbolKind]: GenericObject[SymbolKind]; } export type OmitKind, GenericKindHandler extends KindHandler> = (RemoveKind & { [SymbolKind]: SimplifyTopLevel[SymbolKind], GenericKindHandler["definition"]["name"]>>; }); export declare const keyKindPrefix = "@duplojs/utils/kind/"; type ForbiddenKindCharacters = ForbiddenString; /** * Kinds are a discrimination (type narrowing) mechanism based on adding a marker to an object, but with a few constraints we wanted in our projects: * * Signature: `createKind(name)` and `createKindNamespace(namespace)` → returns a value * * ```ts * const userKind = createKind<"user", { id: string }>("user"); * * interface User extends Kind { * name: string; * } * * const User = { * new(params: { * id: string; * name: string; * }): User { * return userKind.addTo( * { name: params.name }, * { id: params.id }, * ); * }, * is(input: unknown): input is User { * return userKind.has(input); * }, * }; * * const user = User.new({ * id: "u_1", * name: "Ada", * }); * * if (User.is(user)) { * const userId = userKind.getValue(user).id; * } * * const received: unknown = JSON.parse(JSON.stringify(user)); * if (User.is(received)) { * const userId = userKind.getValue(received).id; * } * ``` * * @see https://utils.duplojs.dev/en/v1/api/common/kind * */ export declare function createKind(name: GenericName & ForbiddenKindCharacters): KindHandler>; export interface ReservedKindNamespace { DuplojsUtilsEither: true; DuplojsUtilsDataParser: true; DuplojsUtilsBuilder: true; DuplojsUtilsError: true; DuplojsUtilsClean: true; DuplojsUtilsDate: true; DuplojsUtilsFlow: true; } type ForbiddenKindNamespace = (ForbiddenKindCharacters & ForbiddenString>); /** * Kinds are a discrimination (type narrowing) mechanism based on adding a marker to an object, but with a few constraints we wanted in our projects: * * Signature: `createKind(name)` and `createKindNamespace(namespace)` → returns a value * * ```ts * const userKind = createKind<"user", { id: string }>("user"); * * interface User extends Kind { * name: string; * } * * const User = { * new(params: { * id: string; * name: string; * }): User { * return userKind.addTo( * { name: params.name }, * { id: params.id }, * ); * }, * is(input: unknown): input is User { * return userKind.has(input); * }, * }; * * const user = User.new({ * id: "u_1", * name: "Ada", * }); * * if (User.is(user)) { * const userId = userKind.getValue(user).id; * } * * const received: unknown = JSON.parse(JSON.stringify(user)); * if (User.is(received)) { * const userId = userKind.getValue(received).id; * } * ``` * * @see https://utils.duplojs.dev/en/v1/api/common/kind * */ export declare function createKindNamespace(namespace: GenericNamespace & ForbiddenKindNamespace): (name: GenericName & ForbiddenKindCharacters) => KindHandler>; export type KindHeritageConstructorParams = { [KindHandler in GenericKindHandler as KindHandler["definition"]["name"]]: KindHandler["definition"]["value"]; } extends infer InferredResult extends object ? PartialKeys> : never; /** * @deprecated use kindClass */ export declare function kindHeritage>(uniqueName: GenericUniqueName & ForbiddenKindCharacters, kind: GenericKindHandler | GenericKindHandler[], parent?: GenericParent): new (...args: And<[IsEqual : never, true>, IsEqual, never>]> extends true ? [params?: KindHeritageConstructorParams] : [params: KindHeritageConstructorParams, ...parentArgs: IsEqual, unknown[]> extends true ? [parentParams?: readonly [...ConstructorParameters]] : [parentParams: readonly [...ConstructorParameters]]]) => UnionToIntersection<(GenericKindHandler extends KindHandler ? Kind : never) | Kind> | InstanceType>; export declare function isRuntimeKind(value: string): boolean; export {};