简体   繁体   English

Typescipt:用于在反应中设置默认道具的交集

[英]Typescipt: intersection for setting default props in react

在此处输入图像描述

I want to use intersection so I dont have to explicitly write every type of my defaultProps but rather take it's type from my declared variable, is that possible?我想使用交集,所以我不必显式编写我的 defaultProps 的每种类型,而是从我声明的变量中获取它的类型,这可能吗? As you can see in my code example even thought myProp is set as nullable, it isn't accepting null value正如您在我的代码示例中看到的那样,即使 myProp 设置为可为空,它也不接受 null 值

An intersection combines existing types meaning all the types on both object are present.交集结合了现有类型,这意味着 object 上的所有类型都存在。 You cannot use the type of the declared variable to define testProps if its a union of multiple types, as when you set the default value you bind it to a particular type.如果testProps是多种类型的并集,则不能使用声明变量的类型来定义它,因为当您设置默认值时,会将其绑定到特定类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM