I am faced with the challenge of exporting multiple constants that I will later need to import and iterate through as an array. Due to my use of generics (OptionInterface) and the necessity to maintain type validations, I cannot simply place all constants in one object for export.
Currently, I am exporting each constant as a named export and then exporting them all as an array. However, there is a concern that another developer may overlook the requirement to add a new element to the exported array.
For instance:
//ElemSettings.tsx
export interface ElemProps<OptionInterface = any> {
title: string,
key: string,
options: OptionsInterface[]
export interface SpecialOption {
s1: string,
s2: number
}
//Elements.tsx - desired exports
export const elemOne: ElemProps {
title: 'element One',
key: elemOne'
options: [1,2,3]
}
export const elemTwo: ElemProps {
title: 'element Two',
key: elemTwo'
options: [1,2,3]
}
export const elemThree: ElemProps<SpecialOption> {
title: 'element Two',
key: elemTwo'
options: [{s1:'one',s2:1},{s1:'two',s2:2}]
}
//Current export method
export const elems: Array<ElemProps> =
[elemOne, elemTwo, elemThree]
//user.tsx
import { elems } from 'Elements';
const doSomething = () => {
elems.map(...)
}
If I attempt to simplify the export process by importing all elements, like this:
import * as elems from 'Elements';
and then use it as shown in doSomething function, an error occurs:
Property 'map' does not exist on type 'typeof import("Elements")'.
Do you have any suggestions on how to export all constants as an array while preserving the typing?