我试图弄清楚为什么为React/JSX组件设置子属性会破坏父组件的类型检查。
下面是一个简单的代码示例,如果我没有定义子类型,顶部测试无法在someOtherProp
上显示错误,如果我定义子属性,则第二个测试组件无法在someOtherProp
上显示错误,如果我将子类型设为可选,则两个组件都会失败。
import React from 'react';
export interface TestProps {
style: {
flex?: number;
};
children: React.ReactNode;
}
const Test = ({ style }: TestProps) => <div style={style} />;
export const MainTest = () => (
<>
<Test style={{ flex: 1, someOtherProp: 'center' }} />;
<Test style={{ flex: 1, someOtherProp: 'center' }}>
<p>Children Breaks TypeChecking</p>
</Test>
</>
);
发布于 2019-05-30 07:32:00
这似乎已修复从typescript版本3.4.5升级到3.5.1。
https://stackoverflow.com/questions/56338591
复制相似问题