简体   繁体   English

如何通过 Typescript 项目在 React Native 中使用 AWS Amplify?

[英]How to use AWS Amplify in React Native with Typescript Project?

I'm trying to add Amplify Authentication in my react native project which uses typescript.我正在尝试在使用 typescript 的反应原生项目中添加放大身份验证。 There is a package given in amplify documentation 'aws-amplify-react-native' which is used as a middleware to authenticate our application.放大文档“aws-amplify-react-native”中给出了一个 package,它用作验证我们应用程序的中间件。 But this package is only supported in projects which are based on javascript.但是这个 package 仅在基于 javascript 的项目中受支持。 For Typescript it shows an error like对于 Typescript 它显示如下错误

Could not find a declaration file for module 'aws-amplify-react-native'.
Try `npm install @types/aws-amplify-react-native` if it exists or add a new declaration (.d.ts) file containing `declare module 'aws-amplify-react-native';`

There is no package available like '@types/aws-amplify-react-native'没有 package 可用,例如“@types/aws-amplify-react-native”

So anyone can help me out of this?所以有人可以帮我解决这个问题吗?

Unfortunately there are no defined types at the time of this answer.不幸的是,在此答案时没有定义类型。 However, you can invoke @ts-ignore like so但是,您可以像这样调用@ts-ignore

// @ts-ignore
import Amplify, { Auth } from 'aws-amplify';
// @ts-ignore
import awsconfig from './aws-exports';
// @ts-ignore
import { withAuthenticator } from 'aws-amplify-react-native'

Amplify.configure(awsconfig);

There are no official TypeScript (TS) types available for aws-amplify-react-native at the moment.目前没有可用于aws-amplify-react-native的官方 TypeScript (TS) 类型。 To suppress the TypeScript warning, you need to define your own TS declaration file ( *.d.ts ).要抑制 TypeScript 警告,您需要定义自己的 TS 声明文件 ( *.d.ts )。

To get approximate typings, you can copy this file into your project.要获得近似的类型,您可以将此文件复制到您的项目中。 These are typings written by the GitHub user dantasfiles .这些是 GitHub 用户dantasfiles编写的类型。 Bear in mind that they are not exact and could be specified more precisely.请记住,它们并不精确,可以更精确地指定。

Unfortunately, there is no Typescript configuration option available for React Native AWS amplify till now.不幸的是,到目前为止,还没有可用于 React Native AWS 放大的 Typescript 配置选项。 You can check out the R eact Native AWS amplify documentation for the powerful cloud services it has to offer.您可以查看 R eact Native AWS 放大文档,了解它必须提供的强大云服务。 Also, there are lots of articles on how to integrate the AWS amplify services into your React Native project.此外,还有很多关于如何将 AWS 放大服务集成到您的 React Native 项目中的文章。 Just surf and learn!只是冲浪和学习!

To tell you the truth, there is no Typescript configuration available for React Native AWS amplify to date.说实话,目前还没有可用于 React Native AWS 放大的 Typescript 配置。 You can check out the React Native AWS amplify documentation for all the features and services it has to offer.您可以查看React Native AWS放大文档,了解它必须提供的所有功能和服务。 There is no doubt that it provides some powerful backend cloud services.毫无疑问,它提供了一些强大的后端云服务。 Also, there are lots of articles on how to integrate the AWS amplify services into your React Native project.此外,还有很多关于如何将 AWS 放大服务集成到您的 React Native 项目中的文章。 I advise you to check them out.我建议你检查一下。

I installed this in my typescript project and I have no issue:我在我的 typescript 项目中安装了这个,我没有问题:

 npm install aws-amplify aws-amplify-react-native @react-native-community/netinfo @react-native-async-storage/async-storage

https://docs.amplify.aws/start/getting-started/setup/q/integration/react-native/#initialize-a-new-backend https://docs.amplify.aws/start/getting-started/setup/q/integration/react-native/#initialize-a-new-backend

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

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