@tolki/types 1.1.1 → 1.1.3
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/package.json +1 -1
- package/src/enums.d.ts +92 -11
package/package.json
CHANGED
package/src/enums.d.ts
CHANGED
|
@@ -1,22 +1,30 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Extract the case key names from the _cases tuple.
|
|
3
3
|
*/
|
|
4
|
-
export type CaseKeys<TEnum extends EnumConst> =
|
|
4
|
+
export type CaseKeys<TEnum extends EnumConst> = NonNullable<
|
|
5
|
+
TEnum["_cases"]
|
|
6
|
+
>[number];
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
* Extracts the instance method key names from the `_methods` tuple.
|
|
8
10
|
*/
|
|
9
|
-
export type MethodKeys<TEnum extends EnumConst> =
|
|
11
|
+
export type MethodKeys<TEnum extends EnumConst> = NonNullable<
|
|
12
|
+
TEnum["_methods"]
|
|
13
|
+
>[number];
|
|
10
14
|
|
|
11
15
|
/**
|
|
12
16
|
* Extracts the static helper key names from the `_static` tuple.
|
|
13
17
|
*/
|
|
14
|
-
export type StaticKeys<TEnum extends EnumConst> =
|
|
18
|
+
export type StaticKeys<TEnum extends EnumConst> = NonNullable<
|
|
19
|
+
TEnum["_static"]
|
|
20
|
+
>[number];
|
|
15
21
|
|
|
16
22
|
/**
|
|
17
23
|
* Extracts the helper key names from the `_helpers` tuple.
|
|
18
24
|
*/
|
|
19
|
-
export type HelperKeys<TEnum extends EnumConst> =
|
|
25
|
+
export type HelperKeys<TEnum extends EnumConst> = NonNullable<
|
|
26
|
+
TEnum["_helpers"]
|
|
27
|
+
>[number];
|
|
20
28
|
|
|
21
29
|
/**
|
|
22
30
|
* Constraint for all generated enum const objects.
|
|
@@ -29,9 +37,9 @@ export type HelperKeys<TEnum extends EnumConst> = TEnum["_helpers"][number];
|
|
|
29
37
|
* - `_static` lists static helper method names added to the enum.
|
|
30
38
|
*/
|
|
31
39
|
export type EnumConst = {
|
|
32
|
-
readonly _cases
|
|
33
|
-
readonly _methods
|
|
34
|
-
readonly _static
|
|
40
|
+
readonly _cases?: readonly string[];
|
|
41
|
+
readonly _methods?: readonly string[];
|
|
42
|
+
readonly _static?: readonly string[];
|
|
35
43
|
readonly [key: string]: unknown;
|
|
36
44
|
};
|
|
37
45
|
|
|
@@ -74,6 +82,7 @@ export type ResolveMethodForValue<
|
|
|
74
82
|
* - Static methods / other values: passed through unchanged.
|
|
75
83
|
*/
|
|
76
84
|
export type ResolvedEnumInstance<TEnum extends EnumConst, TValue> = {
|
|
85
|
+
readonly name: MatchedCaseKey<TEnum, TValue>;
|
|
77
86
|
readonly value: TValue;
|
|
78
87
|
} & {
|
|
79
88
|
readonly [K in Exclude<
|
|
@@ -94,7 +103,7 @@ export type CaseValue<TEnum extends EnumConst> = {
|
|
|
94
103
|
/**
|
|
95
104
|
* The fully inferred return type of `from`.
|
|
96
105
|
*/
|
|
97
|
-
export type
|
|
106
|
+
export type FromResult<TEnum extends EnumConst, TValue> =
|
|
98
107
|
MatchedCaseKey<TEnum, TValue> extends never
|
|
99
108
|
? never
|
|
100
109
|
: ResolvedEnumInstance<TEnum, TValue>;
|
|
@@ -122,10 +131,82 @@ export type DefineEnumResult<TEnum extends EnumConst> = Override<
|
|
|
122
131
|
readonly _helpers: string[];
|
|
123
132
|
readonly from: <const TValue extends CaseValue<TEnum>>(
|
|
124
133
|
value: TValue,
|
|
125
|
-
) =>
|
|
134
|
+
) => FromResult<TEnum, TValue>;
|
|
126
135
|
readonly tryFrom: <const TValue extends CaseValue<TEnum>>(
|
|
127
136
|
value: TValue,
|
|
128
|
-
) =>
|
|
129
|
-
readonly cases: () => Array<
|
|
137
|
+
) => FromResult<TEnum, TValue> | null;
|
|
138
|
+
readonly cases: () => Array<FromResult<TEnum, CaseValue<TEnum>>>;
|
|
130
139
|
}
|
|
131
140
|
>;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Keys excluded from the resolved `AsEnum` resource shape.
|
|
144
|
+
* Strips case keys, metadata tuples, and function-valued keys (e.g. `from`,
|
|
145
|
+
* `tryFrom`, `cases` added by `defineEnum`).
|
|
146
|
+
*/
|
|
147
|
+
type AsEnumExcludedKeys<TEnum extends EnumConst> =
|
|
148
|
+
| CaseKeys<TEnum>
|
|
149
|
+
| "_cases"
|
|
150
|
+
| "_methods"
|
|
151
|
+
| "_helpers"
|
|
152
|
+
| "_static"
|
|
153
|
+
| {
|
|
154
|
+
[K in keyof TEnum]: TEnum[K] extends (...args: any[]) => any
|
|
155
|
+
? K
|
|
156
|
+
: never;
|
|
157
|
+
}[keyof TEnum];
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Resolves the `EnumResource` API response shape for a single case key.
|
|
161
|
+
*
|
|
162
|
+
* - `name` and `value` come directly from the case key/value.
|
|
163
|
+
* - Instance methods (listed in `_methods`) are resolved to the value for this
|
|
164
|
+
* specific case via `ResolveMethodForValue`.
|
|
165
|
+
* - Static methods and other properties pass through unchanged.
|
|
166
|
+
* - `backed` is always `boolean` (cannot be inferred from the const type).
|
|
167
|
+
*/
|
|
168
|
+
type AsEnumCase<TEnum extends EnumConst, K extends CaseKeys<TEnum>> = {
|
|
169
|
+
readonly name: K;
|
|
170
|
+
readonly value: TEnum[K];
|
|
171
|
+
readonly backed: boolean;
|
|
172
|
+
} & {
|
|
173
|
+
readonly [P in Exclude<
|
|
174
|
+
keyof TEnum,
|
|
175
|
+
AsEnumExcludedKeys<TEnum>
|
|
176
|
+
>]: P extends MethodKeys<TEnum>
|
|
177
|
+
? ResolveMethodForValue<TEnum, P, TEnum[K]>
|
|
178
|
+
: TEnum[P];
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Resolves the `EnumResource` JSON API response type for a published enum.
|
|
183
|
+
*
|
|
184
|
+
* Produces a **discriminated union** of all possible case shapes, enabling
|
|
185
|
+
* TypeScript narrowing on `name` or `value`.
|
|
186
|
+
*
|
|
187
|
+
* Optionally accepts a second type parameter to pre-narrow to a specific
|
|
188
|
+
* case value, producing a single resolved shape instead of a union.
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* ```ts
|
|
192
|
+
* // Full union — all cases
|
|
193
|
+
* type StatusResource = AsEnum<typeof Status>;
|
|
194
|
+
* // statusResource.value → 0 | 1
|
|
195
|
+
* // statusResource.icon → "pencil" | "check"
|
|
196
|
+
*
|
|
197
|
+
* // Pre-narrowed to a specific value
|
|
198
|
+
* type DraftResource = AsEnum<typeof Status, 0>;
|
|
199
|
+
* // draftResource.value → 0
|
|
200
|
+
* // draftResource.icon → "pencil"
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
export type AsEnum<
|
|
204
|
+
TEnum extends EnumConst,
|
|
205
|
+
TValue extends CaseValue<TEnum> = CaseValue<TEnum>,
|
|
206
|
+
> = {
|
|
207
|
+
[K in CaseKeys<TEnum>]: TEnum[K] extends TValue
|
|
208
|
+
? TValue extends TEnum[K]
|
|
209
|
+
? AsEnumCase<TEnum, K>
|
|
210
|
+
: never
|
|
211
|
+
: never;
|
|
212
|
+
}[CaseKeys<TEnum>];
|