typescript Element implicitly has an ‘any’ type because expression of type X can’t be used to index type Y

Any help to this would be appreciated

interface Error {
  messages: string[];
    entity: 'test' | null;
}

interface GroupedErrors {
  [errorGroup: string]: Error['messages'] | Error[];
}

interface UngroupedErrors {
  ungrouped: Error[];
}

type GroupErrors = GroupedErrors | UngroupedErrors | {};

interface Preset {
  errors: Error[];
}

export function groupPresetErrors(preset: Preset): GroupErrors {
  return preset.errors.reduce<GroupErrors>((acc, error) => {
    if (error.entity === 'test') {
      return {
        ...acc,
        ['test']: acc['test']
          ? [...acc['test'], ...error.messages]
          : error.messages,
      };
    }
    return acc;
  }, {});
}

Element implicitly has an 'any' type because expression of type '"test"' can't be used to index type 'GroupErrors'. Property 'test' does not exist on type 'GroupErrors'.

Playground link



Read more here: https://stackoverflow.com/questions/68474014/typescript-element-implicitly-has-an-any-type-because-expression-of-type-x-can

Content Attribution

This content was originally published by stackjlei at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: