Skip to content
Advertisement

How to type enum and object to have same properties React Typescript

i have an enum:

enum INFO_CODES {
goToInit = 'goToInit',
lockIp = 'lockIp',

}

and i want to create an object(or enum also) with the same properties (only goToInit and lockIp)

const SOME_OBJ = {
goToInit: function, //here i need function
lockIp: function,//here i need function
someProp:function //here i need mistake,can create only like in 
INFO_CODES props.

}

Advertisement

Answer

You can use a Record and just use the enum as the first generic parameter for the key and then whatever function type you want to have next.

enum INFO_CODES {
  goToInit = 'goToInit',
  lockIp = 'lockIp',
}

type result = Record<INFO_CODES, () => void>;

const test: result = {
  goToInit: () => console.log("test"),
  lockIp: () => console.log("lockIp"),
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement