You can do other way around by creating an object with keys of enum:
enum SomethingKeys {
red = "red",
blue= "blue",
green= "green",
}
type ISomething= Record<SomethingKeys, string>
const a: ISomething = {
[SomethingKeys.blue]: 'blue',
[SomethingKeys.red]: 'red',
[SomethingKeys.green]: 'green',
}
But I think what you really need is not enum but union type of keys, what you define by keyof
. Consider:
interface ISomething {
red: string;
blue: string;
green: string;
}
type Keys = keyof ISomething; // "red" | "blue" | "green"
And as you declare yourself as newbie, string literal unions are ok to be used. You don't need enums.
When you have Keys
you can use them to create other types also
// new object with keys of the original one
type OtherTypeWithTheSameKeys = Record<Keys, number> // type with keys of type Keys
const a: OtherTypeWithTheSameKeys = {
blue: 1,
green: 2,
red: 3
}