@tmlmobilidade/go-types-shared 20260615.1437.21 → 20260615.1640.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/utility.d.ts +25 -0
- package/package.json +1 -1
package/dist/utility.d.ts
CHANGED
|
@@ -81,3 +81,28 @@ export type NoneOrExactlyOne<T> = {
|
|
|
81
81
|
}[keyof T] | {
|
|
82
82
|
[K in keyof T]?: never;
|
|
83
83
|
};
|
|
84
|
+
/**
|
|
85
|
+
* Makes keys in `TFields` required when `TCondition` is absent,
|
|
86
|
+
* and optional when `TCondition` is present.
|
|
87
|
+
*
|
|
88
|
+
* @template T - The base object type.
|
|
89
|
+
* @template TCondition - The key that triggers optionality when present.
|
|
90
|
+
* @template TFields - The keys that become optional when `TCondition` is present.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* type Lookup = OptionalIf<
|
|
95
|
+
* { from: string; localField?: string; foreignField?: string; pipeline?: any[] },
|
|
96
|
+
* 'pipeline',
|
|
97
|
+
* 'localField' | 'foreignField'
|
|
98
|
+
* >;
|
|
99
|
+
*
|
|
100
|
+
* const a: Lookup = { from: "c", localField: "x", foreignField: "y" }; // ✅
|
|
101
|
+
* const b: Lookup = { from: "c", pipeline: [] }; // ✅
|
|
102
|
+
* const c: Lookup = { from: "c", pipeline: [], localField: "x" }; // ✅ (optional)
|
|
103
|
+
* const d: Lookup = { from: "c" }; // ❌ (localField/foreignField required when no pipeline)
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
export type OptionalIf<T extends object, TCondition extends keyof T, TFields extends keyof T> = (Omit<T, TCondition | TFields> & Partial<Pick<T, TFields>> & Required<Pick<T, TCondition>>) | (Omit<T, TFields> & Required<Pick<T, TFields>> & {
|
|
107
|
+
[K in TCondition]?: never;
|
|
108
|
+
});
|