import type { SavedGroupsPayload, WidenPrimitives } from "./growthbook"; type OrCondition = { $or: ConditionInterface[]; }; type NorCondition = { $nor: ConditionInterface[]; }; type AndCondition = { $and: ConditionInterface[]; }; type NotCondition = { $not: ConditionInterface; }; /** Names one saved group. Unknown keys are ignored. */ // eslint-disable-next-line @typescript-eslint/no-explicit-any export type SavedGroupReference = { id: string; /** Optional. Checks this attribute instead of the entry's own. */ attributeKey?: AttributePath; }; /** * A reference to one saved group. Top level like `$and`, not scoped to an * attribute. The plural authoring operator `$savedGroups` never reaches an * SDK, so it has no type here. */ export type SavedGroupCondition = { $savedGroup: SavedGroupReference; }; export type Operator = | "$in" | "$ini" | "$inGroup" | "$nin" | "$nini" | "$notInGroup" | "$gt" | "$gte" | "$lt" | "$lte" | "$regex" | "$regexi" | "$ne" | "$eq" | "$size" | "$elemMatch" | "$all" | "$alli" | "$not" | "$type" | "$exists" | "$vgt" | "$vgte" | "$vlt" | "$vlte" | "$vne" | "$veq"; export type VarType = | "string" | "number" | "boolean" | "array" | "object" | "null" | "undefined"; export type ConditionTypeMismatch = { path: string; /** `$eq` for implicit equality */ operator: string; /** Condition value */ expected: unknown; /** Attribute value, `null` when missing */ actual: unknown; }; export type TypeMismatchCallback = (mismatch: ConditionTypeMismatch) => void; export type EvalConditionOptions = { /** Required for saved group operators to match anyone */ savedGroups?: SavedGroupsPayload; onTypeMismatch?: TypeMismatchCallback; }; export type OperatorConditionValue = { $in?: (string | number)[]; $ini?: (string | number)[]; $inGroup?: string; $nin?: (string | number)[]; $nini?: (string | number)[]; $notInGroup?: string; $gt?: number | string; $gte?: number | string; $lt?: number | string; $lte?: number | string; $regex?: string; $regexi?: string; $ne?: number | string; $eq?: number | string; $exists?: boolean; $all?: ConditionValue[]; $alli?: ConditionValue[]; $size?: number | ConditionValue; $type?: VarType; $elemMatch?: ConditionInterface | OperatorConditionValue; $not?: ConditionValue; }; export type ConditionValue = | OperatorConditionValue | string | number | boolean // eslint-disable-next-line | Array // eslint-disable-next-line | Record | null; export type OperatorCondition = { [key: string]: ConditionValue; }; type UntypedCondition = | OrCondition | NorCondition | AndCondition | NotCondition | SavedGroupCondition | OperatorCondition; // With an attributes type, a condition written in code is checked against it: // unknown attributes, and operators or values that don't fit the attribute's // type, are errors. Without one (the default) any condition is accepted. // eslint-disable-next-line @typescript-eslint/no-explicit-any export type ConditionInterface = A extends unknown ? string extends keyof A ? UntypedCondition : { $or?: ConditionInterface[]; $nor?: ConditionInterface[]; $and?: ConditionInterface[]; $not?: ConditionInterface; $savedGroup?: SavedGroupReference; } & { [P in AttributePath]?: AttributeConditionValue>; } : never; type ObjectOf = V extends readonly unknown[] ? never : V extends object ? V : never; type ElementOf = V extends readonly (infer E)[] ? E : never; type Scalar = string | number | boolean; // Adds the operators when some part of the attribute's type supports them type OperatorsFor = [T] extends [never] ? unknown : Operators; /** Attribute names, and dot paths into object attributes (`company.plan`) */ export type AttributePath = A extends unknown ? string extends keyof A ? string : { [K in keyof A & string]: | K // Stops after 4 levels, so a recursive type can't loop forever | (Depth["length"] extends 3 ? never : `${K}.${AttributePath, [...Depth, 0]>}`); }[keyof A & string] : never; // The type of the attribute at a path. `unknown` leaves its conditions unchecked. type AttributeValue = A extends unknown ? P extends keyof A ? A[P] : P extends `${infer K}.${infer Rest}` ? K extends keyof A ? AttributeValue, Rest> : unknown : unknown : never; /** What an attribute of type V can be compared to: a value to equal, or operators */ export type AttributeConditionValue = unknown extends V ? ConditionValue : V | null | AttributeOperators; /** The operators that work on an attribute of type V */ export type AttributeOperators = { $exists?: boolean; $type?: VarType; $not?: AttributeConditionValue; // A missing attribute is null, so any attribute can be compared to null $eq?: Extract | null; $ne?: Extract | null; } & // An array attribute matches when any of its elements is in the list OperatorsFor< Extract, Scalar>, { $in?: (Extract, Scalar> | null)[]; $nin?: (Extract, Scalar> | null)[]; $inGroup?: string; $notInGroup?: string; } > & OperatorsFor< Extract, string>, { $ini?: (Extract, string> | null)[]; $nini?: (Extract, string> | null)[]; } > & OperatorsFor< Extract, { [K in | "$gt" | "$gte" | "$lt" | "$lte" | "$veq" | "$vne" | "$vgt" | "$vgte" | "$vlt" | "$vlte"]?: WidenPrimitives>; } > & OperatorsFor, { $regex?: string; $regexi?: string }> & OperatorsFor< ElementOf, { $size?: AttributeConditionValue; // Operators test each element, and a condition tests an element's fields $elemMatch?: | AttributeOperators> | ConditionInterface>>; $all?: AttributeConditionValue>[]; $alli?: AttributeConditionValue>[]; } >; export type ParentConditionInterface = { id: string; condition: ConditionInterface; gate?: boolean; }; // eslint-disable-next-line export type TestedObj = Record;