Next.js Discord

Discord Forum

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type

Unanswered
American Chinchilla posted this in #help-forum
Open in Discord
American ChinchillaOP
 {Object.entries(keyMapping).map(([key, { text, icon }]) => {
            if (!user.wyniki) return null;

            const value = user.wyniki[key];

            return (
             <div />
            );
          })}


this is the code

this is keyMapping
const keyMapping = {
  BarbellDeadlift: { text: 'Barbell deadlift', icon: '123' },
  BarbellBackSquad: { text: 'Barbell back squad', icon: '' },
  ...
};


this is the type of wyniki attribute:
export interface Records {
  BarbellDeadlift?: number;
  BarbellBenchPress?: number;
  ...
  public?: boolean;
  isVerify: boolean;
  createdAt: string;
  updatedAt: string;
}

2 Replies

@American Chinchilla please keep your questinos focused on Next.js it's more about Typescript
but you can try this
type KeyMapping = {
  [K in keyof Records]?: { text: string; icon: string };
};

const keyMapping: KeyMapping = {
  BarbellDeadlift: { text: 'Barbell deadlift', icon: '123' },
  BarbellBackSquad: { text: 'Barbell back squad', icon: '' },
  // Add other keys as needed
};