I define a set of options represented by strings:
export type Category = 'people' | 'projects' | 'topics' | 'tools'
An index is declared as follows:
interface Entry {
...
}
type IPostEntryIndex = {
[name in Category]: Entry
}
How can I create class properties that correspond to the category names?
const categories: Category[] = ['people', 'projects', 'topics', 'tools']
class PostEntryIndex implements IPostEntryIndex {
// What needs to be included here so that `PostEntryIndex` has a property
// for each category listed in Category?
constructor () {
categories.map(cat => this[cat] = new Entry())
}
}
Note: While I could explicitly declare the categories,
I am seeking a more efficient solution that doesn't require manual updates when a new
category is added. Keeping categories
array aligned with Category
would also be ideal. Perhaps utilizing an enum could help achieve this.