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