代码之家  ›  专栏  ›  技术社区  ›  Albert Gao

如何在Typescript中传输类型

  •  0
  • Albert Gao  · 技术社区  · 6 年前
    interface IDropDownProps {
      items: any[];
      renderItems: (item: any) => React.ReactElement<any>;
    }
    

    renderItems 是一个函数,它接受 items

    我要做的是从 项目 ,所以我可以做一些类似的事情:

    interface IDropDownProps {
      items: Array<T>;
      renderItems: (item: T) => React.ReactElement<any>;
    }
    

    否则,每次声明该类型时,我都需要强制转换该类型 效果图 .

    1 回复  |  直到 6 年前
        1
  •  0
  •   toskv    6 年前

    是的,你可以。你需要定义 但是,在您的界面中的界面级别。

    interface GenericIf<T> { }
    interface IDropDownProps<T> {
        items: Array<T>;
        renderItems: (item: T) => GenericIf<T>;
    }