ChenPt / dailyNote

dailyNode for myself
https://github.com/ChenPt/dailyNote/issues
0 stars 0 forks source link

编写组件说明文件遇到的坑 #38

Open ChenPt opened 4 years ago

ChenPt commented 4 years ago

想要支持组件类作为参数传给组件,一开始编写的是

// index.d.ts
export interface IProps {
  CheckBox?: CheckBox
}

declare class CheckBox extends React.Component<ChexkBoxProps> {
  render(): JSX.Element;
}

// index.tsx
const { CheckBox } = this.props;
<CheckBox />
引用时会提示 `JSX元素类型'CheckBox'没有任何构造或调用签名”`

查询资料得知 组件类作为参数需要如下写法

declare class CheckBox extends React.Component<ChexkBoxProps> {
  render(): JSX.Element;
}

export type CheckBoxComp = CheckBox<ChexkBoxProps>;

export interface IProps {
  CheckBox?: CheckBoxComp; // 将组件类作为参数
}