简体   繁体   English

是否可以使用prop-types来检查常规的Object,Function参数和返回值?

[英]Is it possible to use prop-types to typeCheck regular Object, Function parameters and return value?

是否可以使用prop-types来检查常规Object,Function参数和返回值?

I presume you are talking about using the prop-types package outside of a React application. 我假设您正在谈论在React应用程序之外使用prop-types包。 If that is the case - the answer is yes, but there are limitations. 如果是这样-答案是肯定的,但是有局限性。

To achieve this you need to explicitly call the PropTypes.checkPropTypes function. 为此,您需要显式调用PropTypes.checkPropTypes函数。 This usage is described here . 这里描述这种用法。

To check an object's shape you can use the shapeOf() function. 要检查对象的形状,可以使用shapeOf()函数。 When it comes to functions however, there is no good way I am afraid to check it's parameters or return value. 但是当涉及到函数时,恐怕没有很好的方法来检查其参数或返回值。 There are several closed issues on the prop-types GitHub repository indicating that this is better suited for a type checking system such as Flow or Typescript . prop-types GitHub存储库上有几个未解决的问题,表明它更适合于FlowTypescript之类的类型检查系统。 If you have a very simple setup or you are just getting into the JS ecosystem, I would advise you go with Flow as it is easier to inject into an existing project and can be adopted incrementally. 如果您有一个非常简单的设置,或者只是进入JS生态系统,我建议您使用Flow,因为它更容易注入到现有项目中,并且可以逐步采用。

I hope this helps and answers your question. 希望这对您有所帮助。

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

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