I have developed a method that takes in an object containing data and returns an object that adheres to a specific interface.
interface FireData {
id: EventTypes;
reason?: string;
error?: string;
}
enum EventTypes {
eventType1 = "ev1",
eventType2 = "ev2,
...
}
interface EventComposedData {
key1: string;
key2: boolean;
key3: EventTypes;
"event-value": string;
}
track(fireData: FireData): EventComposedData {
const composedData = composeSomehowECD(fireData);
sendTracking(composedData);
}
I encountered a situation where I needed to provide data in the fireData
object to override the default values returned by composeSomehowECD
.
To achieve this, I proceeded as follows:
interface FireData {
id: EventTypes;
reason?: string;
error?: string;
eventData?: {
[key in keyof EventComposedData]?: any
}
}
Currently, when I execute:
track({
eventData: {
key1: "4"
}
});
key1
has the type any
, which can potentially lead to incorrect values affecting the final output.
I am seeking a way to retrieve all the types of an interface (perhaps something like:
[key in keyof EventComposedData]?: typesof EventComposedData
) or a more efficient method to define the key-value pairs.
One alternative approach could be:
interface FireData {
...
eventData: EventComposedData;
}
However, what if I want to allow properties to be accessed like an index?
For instance:
const eventData: { [key in keyof EventComposedData]: any } = {};
eventData["event-value"]: 5
track(eventData);
In such a scenario, event-value
is always assigned the type any
, causing the same issue as before. It becomes challenging to assign a specific type instead of any
due to the multitude of possible types.
Thank you.