Skip to content

Commit d897bab

Browse files
committed
fix types
1 parent c98fafe commit d897bab

3 files changed

Lines changed: 24 additions & 23 deletions

File tree

dist/index.d.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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
};
139140
export {};

dist/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ function ButterTupleEnum(tuple) {
100100
* @template TEnum The object type containing enum entries
101101
* @returns The keyed enum object with helper methods
102102
*/
103-
function ButterKeyedEnum(enumObject) {
103+
function ButterKeyedEnum(enumObject, options) {
104104
const $enum = (0, deep_freeze_es6_1.default)(Object.fromEntries(Object.entries(enumObject)
105-
.map(([key, value]) => [key, { ...value, key }])));
105+
.map(([key, value]) => [key, { ...value, [options?.keyName ?? "key"]: key }])));
106106
function getMany(keys) {
107107
return keys.map(key => $enum[key]);
108108
}

src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const T extends {
115115
[k: string]: {
116116
[k: string]: any
117117
}
118-
}
118+
} as Readonly<HoistKeyToInner<T, KeyName>>
119119

120120
/**
121121
* Gets multiple values by keys
@@ -126,7 +126,7 @@ const T extends {
126126
function getMany(keys: (keyof TEnum)[]): TEnum[keyof TEnum][];
127127
function getMany(keys: string[]): (TEnum[keyof TEnum] | undefined)[];
128128
function getMany(keys: (keyof TEnum | (string & {}))[]) {
129-
return keys.map(key => $enum[key])
129+
return keys.map(key => $enum[key as keyof TEnum])
130130
}
131131

132132

@@ -137,15 +137,15 @@ const T extends {
137137
*
138138
* @type {Readonly<TEnum>} The enum object with keys hoisted into each value
139139
*/
140-
enum: $enum as Readonly<HoistKeyToInner<T, KeyName>>,
140+
enum: $enum,
141141
/**
142142
* Gets a value by key
143143
*
144144
* @param key The key to retrieve the value for
145145
* @returns {TEnum[keyof TEnum] | undefined} The value for the given key or undefined if the key doesn't exist
146146
*/
147147
get(key: keyof TEnum | (string & {})): TEnum[keyof TEnum] | undefined {
148-
return $enum[key]
148+
return $enum[key as keyof TEnum]
149149
},
150150
getMany,
151151
/**
@@ -154,7 +154,7 @@ const T extends {
154154
* @returns {(keyof TEnum)[]} All keys in the enum
155155
*/
156156
get keys(): (keyof TEnum)[] {
157-
return Object.keys($enum)
157+
return Object.keys($enum) satisfies string[] as (keyof TEnum)[]
158158
},
159159
/**
160160
* All values in the enum
@@ -171,7 +171,7 @@ const T extends {
171171
* @returns {TEnum[keyof TEnum] | undefined} The first value that matches the predicate or undefined if no match is found
172172
*/
173173
find(predicate: (value: TEnum[keyof TEnum], key: keyof TEnum, enumObject: TEnum) => boolean): TEnum[keyof TEnum] | undefined {
174-
return Object.values($enum).find((value, key) => predicate(value, key, $enum))
174+
return Object.values($enum).find((value: any, key: any) => predicate(value, key, $enum)) as any
175175
}
176176
} as const
177177
}

0 commit comments

Comments
 (0)