Repository: strapi/strapi
File: packages/core/admin/admin/src/core/apis/Widgets.ts (TypeScript)

Code before:
  icon?: typeof import('@strapi/icons').PuzzlePiece;
  title: MessageDescriptor;
  link?: {
    label: MessageDescriptor;
    href: To;
  };
  component: () => Promise<React.ComponentType>;
  pluginId?: string;
  id: string;
  permissions?: Array<Pick<Permission, 'action'> & Partial<Omit<Permission, 'action'>>>;
};

type WidgetWithUID = Omit<WidgetArgs, 'id' | 'pluginId'> & { uid: WidgetUID };

type DescriptionReducer<T extends object> = (prev: T[]) => T[];

class Widgets {
  widgets: WidgetArgs[];

  constructor() {
    this.widgets = [];
  }

  private generateUid = (widget: WidgetArgs): WidgetUID => {
    // At this point, widget must be WidgetArgs (has id and pluginId)
    const widgetArgs = widget as WidgetArgs;
    return (
      widgetArgs.pluginId
        ? `plugin::${widgetArgs.pluginId}.${widgetArgs.id}`
        : `global::${widgetArgs.id}`
    ) as WidgetUID;
  };

  private checkWidgets = (widgets: WidgetArgs[]): void => {
    widgets.forEach((widget) => {
      invariant(widget.id, 'An id must be provided');
      invariant(widget.component, 'A component must be provided');
      invariant(widget.title, 'A title must be provided');
      invariant(widget.icon, 'An icon must be provided');
    });
  };

  register(widgets: WidgetArgs): void;
  register(widgets: WidgetArgs[]): void;
  register(widgets: DescriptionReducer<WidgetArgs>): void;
  register(widgets: WidgetArgs | WidgetArgs[] | DescriptionReducer<WidgetArgs>) {
    if (Array.isArray(widgets)) {
      this.checkWidgets(widgets);
      this.widgets = [...this.widgets, ...widgets];
    } else if (typeof widgets === 'function') {
      const newWidgets = widgets(this.widgets);

Review comment (suggestion):
It looks like the type casting here may be left from what you had before, because widget is already of WidgetArgs type here

Code after:
  icon?: typeof import('@strapi/icons').PuzzlePiece;
  title: MessageDescriptor;
  link?: {
    label: MessageDescriptor;
    href: To;
  };
  component: () => Promise<React.ComponentType>;
  pluginId?: string;
  id: string;
  permissions?: Array<Pick<Permission, 'action'> & Partial<Omit<Permission, 'action'>>>;
};

type WidgetWithUID = Omit<WidgetArgs, 'id' | 'pluginId'> & { uid: WidgetUID };

type DescriptionReducer = (prev: WidgetArgs[]) => WidgetArgs[];

class Widgets {
  widgets: WidgetArgs[];

  constructor() {
    this.widgets = [];
  }

  private generateUid = (widget: WidgetArgs): WidgetUID => {
    return widget.pluginId ? `plugin::${widget.pluginId}.${widget.id}` : `global::${widget.id}`;
  };

  private checkWidgets = (widgets: WidgetArgs[]): void => {
    widgets.forEach((widget) => {
      invariant(widget.id, 'An id must be provided');
      invariant(widget.component, 'A component must be provided');
      invariant(widget.title, 'A title must be provided');
      invariant(widget.icon, 'An icon must be provided');
    });
  };

  register(widgets: WidgetArgs | WidgetArgs[] | DescriptionReducer) {
    if (Array.isArray(widgets)) {
      this.checkWidgets(widgets);
      this.widgets = [...this.widgets, ...widgets];
    } else if (typeof widgets === 'function') {
      const newWidgets = widgets(this.widgets);
      this.checkWidgets(newWidgets);
      this.widgets = newWidgets;
    } else if (typeof widgets === 'object') {
      this.checkWidgets([widgets]);
      this.widgets.push(widgets);
    } else {
      throw new Error('Expected widgets to be an array or a reducer function');
    }
  }