@@ -77,28 +77,29 @@ export declare function ButterTupleEnum<const T extends readonly string[]>(tuple
7777 * @template TEnum The object type containing enum entries
7878 * @returns The keyed enum object with helper methods
7979 */
80- export declare function ButterKeyedEnum < const T extends {
81- [ key : string ] : {
82- key ?: never ;
83- [ key : string ] : any ;
84- } ;
85- } > ( enumObject : T ) : {
80+ export declare function ButterKeyedEnum < KeyName extends string = "key" , const T extends {
81+ [ K in keyof T ] : KeyName extends keyof T [ K ] ? never : Record < string , any > ;
82+ } = {
83+ [ key : string ] : any ;
84+ } > ( enumObject : T , options ?: {
85+ keyName ?: KeyName ;
86+ } ) : {
8687 /**
8788 * The enum object
8889 *
8990 * @type {Readonly<TEnum> } The enum object with keys hoisted into each value
9091 */
91- readonly enum : Readonly < HoistKeyToInner < T > > ;
92+ readonly enum : Readonly < HoistKeyToInner < T , KeyName > > ;
9293 /**
9394 * Gets a value by key
9495 *
9596 * @param key The key to retrieve the value for
9697 * @returns {TEnum[keyof TEnum] | undefined } The value for the given key or undefined if the key doesn't exist
9798 */
98- readonly get : ( key : keyof T | ( string & { } ) ) => Readonly < HoistKeyToInner < T > > [ keyof T ] | undefined ;
99+ readonly get : ( key : keyof T | ( string & { } ) ) => Readonly < HoistKeyToInner < T , KeyName > > [ keyof T ] | undefined ;
99100 readonly getMany : {
100- ( keys : ( keyof T ) [ ] ) : Readonly < HoistKeyToInner < T > > [ keyof T ] [ ] ;
101- ( keys : string [ ] ) : ( Readonly < HoistKeyToInner < T > > [ keyof T ] | undefined ) [ ] ;
101+ ( keys : ( keyof T ) [ ] ) : Readonly < HoistKeyToInner < T , KeyName > > [ keyof T ] [ ] ;
102+ ( keys : string [ ] ) : ( Readonly < HoistKeyToInner < T , KeyName > > [ keyof T ] | undefined ) [ ] ;
102103 } ;
103104 /**
104105 * All keys in the enum
@@ -111,14 +112,14 @@ export declare function ButterKeyedEnum<const T extends {
111112 *
112113 * @returns {TEnum[keyof TEnum][] } All values in the enum
113114 */
114- readonly values : Readonly < HoistKeyToInner < T > > [ keyof T ] [ ] ;
115+ readonly values : Readonly < HoistKeyToInner < T , KeyName > > [ keyof T ] [ ] ;
115116 /**
116117 * Finds a value by predicate
117118 *
118119 * @param predicate A function that tests each value for a condition
119120 * @returns {TEnum[keyof TEnum] | undefined } The first value that matches the predicate or undefined if no match is found
120121 */
121- readonly find : ( predicate : ( value : Readonly < HoistKeyToInner < T > > [ keyof T ] , key : keyof T , enumObject : Readonly < HoistKeyToInner < T > > ) => boolean ) => Readonly < HoistKeyToInner < T > > [ keyof T ] | undefined ;
122+ readonly find : ( predicate : ( value : Readonly < HoistKeyToInner < T , KeyName > > [ keyof T ] , key : keyof T , enumObject : Readonly < HoistKeyToInner < T , KeyName > > ) => boolean ) => Readonly < HoistKeyToInner < T , KeyName > > [ keyof T ] | undefined ;
122123} ;
123124/**
124125 * Utility type that hoists the key name into each value object
@@ -129,11 +130,11 @@ export declare function ButterKeyedEnum<const T extends {
129130 * @template T The input object type with nested objects as values
130131 * @returns A type with the same structure but with keys hoisted into each value
131132 */
132- type HoistKeyToInner < T > = {
133+ type HoistKeyToInner < T , KeyName extends string = "key" > = {
133134 [ K in keyof T ] : {
134- [ P in keyof T [ K ] as P extends "key" ? never : P ] : T [ K ] [ P ] ;
135+ [ P in keyof T [ K ] as P extends KeyName ? never : P ] : T [ K ] [ P ] ;
135136 } extends infer O ? {
136- [ P in keyof O | "key" ] : P extends keyof O ? O [ P ] : K ;
137+ [ P in keyof O | KeyName ] : P extends keyof O ? O [ P ] : K ;
137138 } : never ;
138139} ;
139140export { } ;
0 commit comments