【发布时间】:2018-12-10 02:09:33
【问题描述】:
我已经将 prop children 描述为可选,但它具有默认 prop,甚至它已声明 Typescript 会引发错误
import React, { MouseEvent } from 'react';
const defaultProps = {
children: () => null,
};
type TMouseCoordinates = { x: number, y: number };
type TProps = Partial<
{
children?: (coordinates: TMouseCoordinates) => JSX.Element | null;
} & TDefaultProps
>;
class MouseCoordinates extends React.Component<TProps, TState> {
static readonly defaultProps: TProps = defaultProps;
render() {
const { children } = this.props;
const isRenderIsFunction = typeof this.props.children === 'function';
return isRenderIsFunction ? children(this.state) : null;
^^^^^^^^
}
}
无法调用可能是“未定义”的对象。
【问题讨论】:
标签: typescript