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

Issue

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.

}

Solution

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"),
}

Answered By – Mushroomator

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published