首页 > 解决方案 > 打字稿递归箭头函数类型

问题描述

我有这两个功能

  const renderEntry = (entry: NavigationEntryType, index: number): ReactElement => {
    const { display_name: displayName } = entry;

    const id = displayName.replace(' ', '');

    return <NavigationEntry key={id} index={index} displayName={displayName} id={id} moveEntry={moveEntry} />;
  };

  const iterateItems = <T extends NavigationEntryType[]>(items: T): T | ReactElement => {
    return items.map((item, index) => {
      if (item.items && item.items.length > 0) {
        return iterateItems(item.items);
      }

      return renderEntry(item, index);
    }
  );

我收到关于以下返回类型的错误iterateItems

Error:(33, 5) TS2322: Type '(NavigationEntryType[] | ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<...>)>)[]' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | T'.
  Type '(NavigationEntryType[] | ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<...>)>)[]' is not assignable to type 'T'.

应该是什么?

试过了,T | ReactElement但这不起作用,我对打字稿很陌生。

标签: javascriptreactjstypescriptecmascript-6types

解决方案


推荐阅读