@guillaume-docquier/tools-ts 1.2.1 → 1.3.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.
- package/dist/utility-types.d.ts +22 -0
- package/package.json +1 -1
package/dist/utility-types.d.ts
CHANGED
|
@@ -139,3 +139,25 @@ export type PartialProperties<TObject, TKey extends keyof TObject> = Omit<TObjec
|
|
|
139
139
|
* ```
|
|
140
140
|
*/
|
|
141
141
|
export type ValueOf<T> = T[keyof T];
|
|
142
|
+
/**
|
|
143
|
+
* Creates an enum type out of a const object.
|
|
144
|
+
* Useful for projects that run native Typescript in node and can't use enums, but want the same ergonomics.
|
|
145
|
+
* This is basically an alias of {@link ValueOf}.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* type MyEnum = Enumify<MyEnum>
|
|
150
|
+
* ^? "a" | "b" | "c"
|
|
151
|
+
* const MyEnum = {
|
|
152
|
+
* A: "a",
|
|
153
|
+
* B: "b",
|
|
154
|
+
* C: "c",
|
|
155
|
+
* } as const
|
|
156
|
+
*
|
|
157
|
+
* function useEnum(value: MyEnum) {}
|
|
158
|
+
*
|
|
159
|
+
* useEnum(MyEnum.A) // works
|
|
160
|
+
* useEnum("a") // also works
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
export type Enumify<TObject extends Record<string, string>> = ValueOf<TObject>;
|