简体   繁体   English

用于打字稿问题的 AWS Amplify @aws-amplify/ui-react Authenticator 功能道具类型

[英]AWS Amplify @aws-amplify/ui-react Authenticator function prop types for typescript issues

Spent quite sometime trying to use the relevant types in my aws amplify app which uses the amplify auth.花了很长时间尝试在我的 aws amplify 应用程序中使用相关类型,该应用程序使用 amplify auth。

From the @aws-amplify/ui-react module been struggling to type the props correctly. @aws-amplify/ui-react 模块一直在努力正确输入道具。

documented use case:记录的用例:

<Authenticator>
  {({ signOut, user }) => (
      <main>
          <h1>Hello {user?.username}</h1>
          <button onClick={signOut}>Sign out</button>
      </main>
  )}
</Authenticator>

I would like to type the signOut and user props respectively.我想分别输入 signOut 和 user 道具。 The IDE suggests that the props should be typed like so: IDE 建议应该像这样键入道具:

signOut is ((data?: (AuthEventData | undefined)) => void) | undefined signOut 是((data?: (AuthEventData | undefined)) => void) | undefined ((data?: (AuthEventData | undefined)) => void) | undefined

user is CognitoUserAmplify用户是CognitoUserAmplify

Therefore importing these from the aws-amplify modules throws the following errors:因此,从 aws-amplify 模块导入这些会引发以下错误:

Module '"aws-amplify"' has no exported member 'AuthEventData模块 '"aws-amplify"' 没有导出的成员 'AuthEventData

Module '"aws-amplify"' has no exported member 'CognitoUserAmplify'模块“aws-amplify”没有导出成员“CognitoUserAmplify”

What is the recommendation approach if one seeks to add types?如果要添加类型,推荐方法是什么? All documented demos are in javaScript and no reference to typescript types or typings.所有记录在案的演示都在 JavaScript 中,并且没有提及打字稿类型或类型。

I am not sure what the recommended approach is, but I solved it by:我不确定推荐的方法是什么,但我通过以下方式解决了它:

import { AuthEventData, CognitoUserAmplify } from '@aws-amplify/ui-react/node_modules/@aws-amplify/ui';

type AppProps = {
    signOut: ((data?: (AuthEventData | undefined)) => void) | undefined;
    user: CognitoUserAmplify | undefined;
};

const App = ({ signOut, user }: AppProps) => {
...
}

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

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