【发布时间】:2019-02-26 08:13:48
【问题描述】:
如果我有这样定义的道具:
interface Props {
Layout?: LayoutComponent;
}
如果我在 ClassComponent 上提供 defaultProps:
class MyComp extends React.Component<Props> {
static defaultProps: Pick<Props, 'Layout'> = {
Layout: ({ children }) => <>{children}</>
};
}
Typescript 没有接受它不能被取消定义的事实:
render() {
const { previousLocation, data } = this.state;
const { location, Layout } = this.props;
const initialData = this.prefetcherCache[location.pathname] || data;
return (
<Layout> // JSX element type 'Layout' does not have any construct or call signatures.
<Switch>
它不应该因为defaultProps 而未定义。 Typescript 不支持这个。
有没有办法让编译器知道它不能被取消定义?
【问题讨论】:
标签: reactjs typescript