@gateweb/react-utils 1.3.0 → 1.4.0

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.
@@ -50,5 +50,43 @@ type Entries<T> = {
50
50
  type MapToString<T> = {
51
51
  [K in keyof T]: string;
52
52
  };
53
+ /**
54
+ * A utility type that ensures at least one property in the generic type `T` is required.
55
+ *
56
+ * @template T - The object type to ensure at least one property is required.
57
+ *
58
+ * @example
59
+ *
60
+ * type Example = { a: string; b: string };
61
+ * type AtLeastOneExample = AtLeastOne<Example>;
62
+ *
63
+ * const obj: AtLeastOneExample = { a: 'hello' }; // OK
64
+ * const obj2: AtLeastOneExample = { b: 'world' }; // OK
65
+ * const obj3: AtLeastOneExample = { a: 'hello', b: 'world' }; // OK
66
+ * const obj4: AtLeastOneExample = {}; // Error
67
+ */
68
+ type AtLeastOne<T, Keys extends keyof T = keyof T> = Keys extends keyof T ? Partial<Pick<T, Exclude<keyof T, Keys>>> & Required<Pick<T, Keys>> : never;
69
+ /**
70
+ * A utility type that ensures only one property in the generic type `T` is required.
71
+ *
72
+ * @template T - The object type to ensure only one property is required.
73
+ *
74
+ * @example
75
+ *
76
+ * type Example = { a: string; b: string };
77
+ * type OnlyOneExample = OnlyOne<Example>;
78
+ *
79
+ * const obj: OnlyOneExample = { a: 'hello' }; // OK
80
+ * const obj2: OnlyOneExample = { b: 'world' }; // OK
81
+ * const obj3: OnlyOneExample = { a: 'hello', b: 'world' }; // Error
82
+ * const obj4: OnlyOneExample = {}; // Error
83
+ */
84
+ type OnlyOne<T> = {
85
+ [K in keyof T]: {
86
+ [P in K]: T[P];
87
+ } & {
88
+ [P in Exclude<keyof T, K>]?: never;
89
+ };
90
+ }[keyof T];
53
91
 
54
- export type { Entries, MapToString, TExtractValueType };
92
+ export type { AtLeastOne, Entries, MapToString, OnlyOne, TExtractValueType };
@@ -50,5 +50,43 @@ type Entries<T> = {
50
50
  type MapToString<T> = {
51
51
  [K in keyof T]: string;
52
52
  };
53
+ /**
54
+ * A utility type that ensures at least one property in the generic type `T` is required.
55
+ *
56
+ * @template T - The object type to ensure at least one property is required.
57
+ *
58
+ * @example
59
+ *
60
+ * type Example = { a: string; b: string };
61
+ * type AtLeastOneExample = AtLeastOne<Example>;
62
+ *
63
+ * const obj: AtLeastOneExample = { a: 'hello' }; // OK
64
+ * const obj2: AtLeastOneExample = { b: 'world' }; // OK
65
+ * const obj3: AtLeastOneExample = { a: 'hello', b: 'world' }; // OK
66
+ * const obj4: AtLeastOneExample = {}; // Error
67
+ */
68
+ type AtLeastOne<T, Keys extends keyof T = keyof T> = Keys extends keyof T ? Partial<Pick<T, Exclude<keyof T, Keys>>> & Required<Pick<T, Keys>> : never;
69
+ /**
70
+ * A utility type that ensures only one property in the generic type `T` is required.
71
+ *
72
+ * @template T - The object type to ensure only one property is required.
73
+ *
74
+ * @example
75
+ *
76
+ * type Example = { a: string; b: string };
77
+ * type OnlyOneExample = OnlyOne<Example>;
78
+ *
79
+ * const obj: OnlyOneExample = { a: 'hello' }; // OK
80
+ * const obj2: OnlyOneExample = { b: 'world' }; // OK
81
+ * const obj3: OnlyOneExample = { a: 'hello', b: 'world' }; // Error
82
+ * const obj4: OnlyOneExample = {}; // Error
83
+ */
84
+ type OnlyOne<T> = {
85
+ [K in keyof T]: {
86
+ [P in K]: T[P];
87
+ } & {
88
+ [P in Exclude<keyof T, K>]?: never;
89
+ };
90
+ }[keyof T];
53
91
 
54
- export type { Entries, MapToString, TExtractValueType };
92
+ export type { AtLeastOne, Entries, MapToString, OnlyOne, TExtractValueType };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gateweb/react-utils",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "React Utils for GateWeb",
5
5
  "homepage": "https://github.com/GatewebSolutions/react-utils",
6
6
  "files": [