Imagine having this AnalyticsService class
export class AnalyticsService {
static sendAnalytics(eventName: string) {
console.log(eventName);
// logic here...
}
static EVENTS = {
Header: {
LogoClicked: "Header: Logo Clicked",
},
UserMenu: {
LoginButtonClicked: "User Menu: Login Button Clicked",
LogoutButtonClicked: "User Menu: Logout Button Clicked",
}
};
}
Using this class to send analytics like this:
AnalyticsService.sendAnalytics(AnalyticsService.EVENTS.Header.LogoClicked)
The goal is to gather all values of EVENTS
into a union type
to ensure that sendAnalytics
function receives only valid event names
For instance, the expected results would be:
"Header: Logo Clicked" | "User Menu: Login Button Clicked" | "User Menu: Logout Button Clicked"
Is it feasible with typescript?
If so, will it have a notable impact on performance when dealing with a large object in TypeScript?
Edit: just to clarify the EVENTS
object can be deeply nested (I provided a simple example for clarity)