Reputation: 2795
I am new to typescript. I am not 100% clear how to explain this.
I have an enum like this.
export enum SoundTypes {
NORMAL_REEL_SPIN,
BUTTON_CLICK,
}
I have another hold details like this.
static AudioFileInfo: { [id: string]: IAudioFileData } = {
"NORMAL_REEL_SPIN": {
fileName: "reelspin_music",
volume: 0.1,
inPrimary: true
},
"BUTTON_CLICK": {
fileName: "El_ButtonGeneral1",
volume: 0.1,
inPrimary: true
},
}
As you can see the type for the id string value is same as enum.
Inside the application at various places it select what type of music want to play. Using that type I find details about audio file to play.
what i want to do is create something like this; instead of id type string i want to use SoundTypes enum.
static AudioFileInfo: { [id: SoundTypes ]: IAudioFileData } = {
Is there a way to do something similar?
Upvotes: 0
Views: 68
Reputation: 249606
You can use a mapped type to map over the enum values. In this case the predefined mapped type Record
should do:
export enum SoundTypes {
NORMAL_REEL_SPIN,
BUTTON_CLICK,
}
type IAudioFileData = {}
const AudioFileInfo: Record<SoundTypes, IAudioFileData> = {
[SoundTypes.NORMAL_REEL_SPIN]: {
fileName: "reelspin_music",
volume: 0.1,
inPrimary: true
},
[SoundTypes.BUTTON_CLICK]: {
fileName: "El_ButtonGeneral1",
volume: 0.1,
inPrimary: true
},
}
Upvotes: 1