File tree Expand file tree Collapse file tree 2 files changed +22
-4
lines changed
Expand file tree Collapse file tree 2 files changed +22
-4
lines changed Original file line number Diff line number Diff line change @@ -8,4 +8,11 @@ describe("Enum", () => {
88 it ( "returns an object with values equal to keys given a single string" , ( ) => {
99 expect ( Enum ( "BLACK" ) ) . toEqual ( { BLACK : "BLACK" } ) ;
1010 } ) ;
11+
12+ it ( "returns the original object given an object" , ( ) => {
13+ expect ( Enum ( {
14+ BLACK : "black" ,
15+ WHITE : "white" ,
16+ } ) ) . toEqual ( { BLACK : "black" , WHITE : "white" } ) ;
17+ } ) ;
1118} ) ;
Original file line number Diff line number Diff line change 1- export function Enum < V extends string > ( ...values : V [ ] ) : { [ K in V ] : K } {
2- const result : any = { } ;
3- values . forEach ( ( value ) => result [ value ] = value ) ;
4- return result ;
1+ export function Enum < V extends string > ( ...values : V [ ] ) : { [ K in V ] : K } ;
2+ export function Enum <
3+ T extends { [ _ : string ] : V } ,
4+ V extends string
5+ > ( definition : T ) : T ;
6+ export function Enum ( ...values : any [ ] ) : any {
7+ if ( typeof values [ 0 ] === "string" ) {
8+ const result : any = { } ;
9+ for ( const value of values ) {
10+ result [ value ] = value ;
11+ }
12+ return result ;
13+ } else {
14+ return values [ 0 ] ;
15+ }
516}
617
718export type Enum < T > = T [ keyof T ] ;
You can’t perform that action at this time.
0 commit comments