[英]How to use React Native with Typescript?
I've reading that React Native supports Typescript without dependencies like react-native-typescript-transformer . 我已经读到React Native支持Typescript而没有像react-native- typescript -transformer这样的依赖项。 How do I configure it to code using Typescript ?
如何配置它以使用Typescript进行编码?
With the upgrade to Babel v7, Typescript is supported but you will still need to make some changes to your application: 随着Babel v7的升级,支持Typescript,但是您仍然需要对应用程序进行一些更改:
@types/jest @types/react @types/react-native @types/react-test-renderer
@types/jest @types/react @types/react-native @types/react-test-renderer
.js
files to .ts
or .tsx
depending on whether such files contain JSX or not. .js
文件更改为.ts
或.tsx
具体取决于此类文件是否包含JSX。 Note : keep index.js
as is, otherwise Metro builder will fail since it is still not updated to expect a .ts
entry file. index.js
不变,否则Metro生成器将失败,因为它仍未更新为期望包含.ts
条目文件。 For testing, Jest must be made aware of Typescript by adding the following to its configuration section in package.json
: 为了进行测试,必须通过在
package.json
配置部分中添加以下内容来使Jest了解Typescript:
"jest": { "preset": "react-native", "moduleFileExtensions": [ "ts", "tsx", "js" ], "testRegex": "(/__tests__/.*|\\\\.(test|spec))\\\\.(ts|tsx|js)$" }
Be aware that with this setup Babel just transforms Typescript to Javascript, it does not perform type-checking. 请注意,通过此设置,Babel只会将Typescript转换为Javascript,它不会执行类型检查。
Also, expect possible issue since it is the first React Native release to support Typescript. 另外,因为这是第一个支持Typescript的React Native版本,所以可能会出现问题。 My recommendation is to still follow the instructions below until things stabilize with time and patches.
我的建议是仍然遵循以下说明,直到随着时间和补丁而稳定下来。
React Native does not offer first-party Typescript support. React Native不提供第一方Typescript支持。 Instead, it offers Flow support out-of-the-box if you want types without having to add additional dependencies (other than
flow-bin
of course). 相反,如果您想要类型而不必添加其他依赖项(当然不是
flow-bin
),则它提供了开箱即用的Flow支持。
If you necessarily want Typescript, you will need to add some dependencies one way or another, depending on the setup you would eventually pick to use. 如果您一定要使用Typescript,则需要根据最终选择使用的设置,以一种或另一种方式添加一些依赖项。 There is a React Native blog post Using Typescript with React Native that explains one way to add Typescript.
有一篇React Native博客文章将Typescript与React Native结合使用 ,其中介绍了一种添加Typescript的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.