简体   繁体   English

如何在Typescript中使用React Native?

[英]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进行编码?

Since 0.57.0 由于0.57.0

With the upgrade to Babel v7, Typescript is supported but you will still need to make some changes to your application: 随着Babel v7的升级,支持Typescript,但是您仍然需要对应用程序进行一些更改:

  • Install type definition dependencies @types/jest @types/react @types/react-native @types/react-test-renderer 安装类型定义依赖项@types/jest @types/react @types/react-native @types/react-test-renderer
  • Change your .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条目文件。
  • You will need to add module declarations for binary assets like images, videos and json files for Typescript to understand how to import them (example here ). 您将需要为Typescript的图像,视频和json文件等二进制资产添加模块声明,以了解如何导入它们( 此处为示例)。
  • 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. 我的建议是仍然遵循以下说明,直到随着时间和补丁而稳定下来。

Before 0.57.0 之前0.57.0

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.

相关问题 如何在Typescript中使用react-native-web - How to use react-native-web with Typescript React Native with typescript - 如何使用来自@react-navigation/native 的 useRoute with typescript - React native with typescript - how to use the useRoute from @react-navigation/native with typescript 如何通过 Typescript 项目在 React Native 中使用 AWS Amplify? - How to use AWS Amplify in React Native with Typescript Project? 如何在 TypeScript react-native 中使用固有属性作为类型 - How to use an Intrinsic attributes as types in TypeScript react-native React Native + Typescript:如何在堆栈屏幕组件中使用 toggleDrawer? - React Native + Typescript: How to use toggleDrawer in a stack screen component? 如何在 Expo SDK 40 中使用 SVG 与 Z558B544CF68335F39D386EZC403 反应原生? - How to use SVG in Expo SDK 40 React Native with TypeScript? 如何在 React Native typescript 类组件中使用扩展运算符 - How to use spread operator in React native typescript class component 如何使用 TypeScript 在 React Native 中将 forwardRef 与 FunctionComponent 一起使用 - How to use forwardRef with FunctionComponent in React Native using TypeScript 如何在 typescript 中使用 react-native-root-toast 作为组件 - how to use react-native-root-toast as component in typescript React-native:如何在React-native中将jsx打字稿.tsx文件使用(和翻译)? - React-native: How to use (and translate) typescript .tsx files with jsx in React-native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM