Encountering an issue with TypeScript that involves a class property typed as AmplitudeFeatureFlags | {} = {}; initialized as an empty object. Attempting to retrieve a value from this class property using a method that takes in a property name argument of enum type, but running into errors as shown below.
Seeking guidance on how to resolve this problem.
export enum AmplitudeFeatureFlagNames {
addTeamMembersFromUserMenu = 'addTeamMembersFromUserMenu',
inAppHandRaiser = 'inAppHandRaiser',
}
type AmplitudeFeatureFlagWithPayload<Payload extends object = {}> = {
key: string;
payload?: Payload;
};
export type AmplitudeFeatureFlags = {
[AmplitudeFeatureFlagNames.addTeamMembersFromUserMenu]: AmplitudeFeatureFlagWithPayload;
[AmplitudeFeatureFlagNames.inAppHandRaiser]: AmplitudeFeatureFlagWithPayload;
};
class User {
private amplitudeFeatureFlags: AmplitudeFeatureFlags | {} = {};
getAmplitudeFeatureFlagPayload(flagName: AmplitudeFeatureFlagNames) {
if (flagName in this.amplitudeFeatureFlags) {
**//ERROR on the line below:**
//Element implicitly has an 'any' type because expression of type
//'AmplitudeFeatureFlagNames' can't be used to index type '{} |
//AmplitudeFeatureFlags'.
//Property '[AmplitudeFeatureFlagNames.addTeamMembersFromUserMenu]' does not exist on
//type '{} | AmplitudeFeatureFlags'.
**return this.amplitudeFeatureFlags[flagName]?.payload;**
}
}
}