@tolki/types 1.1.2 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/enums.d.ts +77 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tolki/types",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "Utility TypeScript types for @tolki packages and Laravel HTTP responses.",
5
5
  "author": "Abraham Arango <https://github.com/abetwothree>",
6
6
  "license": "MIT",
package/src/enums.d.ts CHANGED
@@ -82,6 +82,7 @@ export type ResolveMethodForValue<
82
82
  * - Static methods / other values: passed through unchanged.
83
83
  */
84
84
  export type ResolvedEnumInstance<TEnum extends EnumConst, TValue> = {
85
+ readonly name: MatchedCaseKey<TEnum, TValue>;
85
86
  readonly value: TValue;
86
87
  } & {
87
88
  readonly [K in Exclude<
@@ -102,7 +103,7 @@ export type CaseValue<TEnum extends EnumConst> = {
102
103
  /**
103
104
  * The fully inferred return type of `from`.
104
105
  */
105
- export type ToEnumResult<TEnum extends EnumConst, TValue> =
106
+ export type FromResult<TEnum extends EnumConst, TValue> =
106
107
  MatchedCaseKey<TEnum, TValue> extends never
107
108
  ? never
108
109
  : ResolvedEnumInstance<TEnum, TValue>;
@@ -130,10 +131,82 @@ export type DefineEnumResult<TEnum extends EnumConst> = Override<
130
131
  readonly _helpers: string[];
131
132
  readonly from: <const TValue extends CaseValue<TEnum>>(
132
133
  value: TValue,
133
- ) => ToEnumResult<TEnum, TValue>;
134
+ ) => FromResult<TEnum, TValue>;
134
135
  readonly tryFrom: <const TValue extends CaseValue<TEnum>>(
135
136
  value: TValue,
136
- ) => ToEnumResult<TEnum, TValue> | null;
137
- readonly cases: () => Array<ToEnumResult<TEnum, CaseValue<TEnum>>>;
137
+ ) => FromResult<TEnum, TValue> | null;
138
+ readonly cases: () => Array<FromResult<TEnum, CaseValue<TEnum>>>;
138
139
  }
139
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>];