简体   繁体   English

与Typescript互动:使用Typescript可以代替propTypes定义吗?

[英]React with Typescript: Should the use of Typescript replace propTypes definitions?

I'm new to Typescript and after reading some related articles it seems that by using Typescript in React, propTypes definitions are no longer needed. 我是Typescript的新手,在阅读了一些相关文章之后,似乎通过在React中使用Typescript,不再需要propTypes定义。

But by checking some of the most populars React Component Libraries projects 但是通过检查一些最受欢迎的React Component Libraries项目

like Material-IU : Material-IU一样

or Semantic UI React 语义UI React

or React Toolbox React工具箱

...it seems some big projects are using both. ...似乎一些大型项目同时使用了这两种方法。 So, i'm a bit confused about this. 所以,我对此有些困惑。 And i don't really get the purpose of these *.d.ts files 而且我并没有真正了解这些* .d.ts文件的目的

Why are they using both? 他们为什么同时使用两者? What is the purpose of the *.d.ts files? * .d.ts文件的目的是什么? How does this fit in a react development workflow? 这如何适合于React开发工作流程?

My actual idea about this this is that: 我对此的实际想法是:

  • *.d.ts should contain the types for the component's props * .d.ts应包含组件道具的类型
  • typescript will launch compilation errors if some prop is receiving a wrong type of data 如果某些道具接收到错误类型的数据,则打字稿将启动编译错误
  • the React component definition can avoid defining the propTypes (as this is handled now by Typescript in a different file) React组件定义可以避免定义propTypes(因为TypeScript现在可以在其他文件中处理该属性)

Is this correct? 这个对吗? Can anyone shed some light on this? 谁能对此有所启发? :) :)

Thanks in advance!! 提前致谢!!

The TypeScript definition ( .d.ts ) files define the type information used by the TypeScript compiler, and in many IDEs these files are also used to provide feedback during development. TypeScript定义( .d.ts )文件定义了TypeScript编译器使用的类型信息,并且在许多IDE中,这些文件还用于在开发过程中提供反馈。 Aside from the definitions provided by vendors, you can use interface s when writing your React components in TypeScript. 除了供应商提供的定义外,您还可以在TypeScript中编写React组件时使用interface A common use case is to define the type of the props object. 一个常见的用例是定义props对象的类型。

One benefit of using a interface, as opposed to defining propTypes , is that this code disappears at runtime, so if you pass the right props you don't need to run any type checks in the compiled version of your app. 与定义propTypes ,使用接口的一个好处是该代码在运行时消失了,因此,如果传递正确的props,则无需在应用程序的编译版本中运行任何类型检查。

That said, there are situations where it is still helpful to check the type of props passed to a component at runtime. 也就是说,在某些情况下,在运行时检查传递给组件的道具类型仍然很有帮助。 For example, maybe you are using data from an external API and wanted to make sure that the format is what you expected. 例如,也许您正在使用来自外部API的数据,并且想要确保格式符合您的期望。 In this case, it would make sense to use propTypes . 在这种情况下,使用propTypes

Also, as rightly pointed out in the comments, the propTypes are useful when the library is consumed by code written in JavaScript, which would use a compiled version of the code that would otherwise lack the type information. 而且,正如注释中正确指出的那样,当库被用JavaScript编写的代码使用时, propTypes很有用,这些JavaScript将使用代码的编译版本,否则将缺少类型信息。

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

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