Skip to content

组件类型标注相关 #18

@ImJustAMan

Description

@ImJustAMan

现在暂时只有List遇到了这个问题 由于List组件本身同时要支持拥有一个子组件+ref
所以需要由基本类型标注方法
React.FC<IComponentPropsType>
改为
React.FC<IComponentPropsType & React.RefAttributes<HTMLElement>> & { Item?: typeof ChildComponent }
此段类型为 React.FC<IComponentPropsType & React.RefAttributes<HTMLElement>> 用于标注React函数式组件类型
同时内部IComponentPropsType & React.RefAttributes<HTMLElement>为组件props类型 + 对应ref类型
后半段{ Item?: typeof ChildComponent }为对应子组件类型 如果遇到类似的操作可以考虑这种用法
但是同时子组件无法直接使用 因为此段子组件只能为可选属性 否则组件本身类型报错
此时有两种情况

  1. 如果使用解构
    const { Item } = List;
    const MenuItem = Item as typeof ListItem
  2. 不使用解构
    const Item = List.Item as typeof ListItem

暂时只有这种解决方案 如有更好方案欢迎在此issue下交流

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussionDiscuss idea or feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions