英文:
Property 'signInUserSession' does not exist on type 'AmplifyUser' but exists at runtime
问题
I'm using the useAuthenticator
hook from AWS Amplify and I'm confused why certain properties do not exist at compile time. I get the error Property 'signInUserSession' does not exist on type 'AmplifyUser'
. However, when setting a debugger, I get to see that it does exist at runtime.
import { useAuthenticator } from "@aws-amplify/ui-react";
const { route, user } = useAuthenticator((context) => [context.route, context.user]);
const userGroup = user.signInUserSession.getIdToken.payload['cognito:groups'];
// no property 'signInUserSession'
const userGroup = user.getSignInUserSession()?.getIdToken.payload['cognito:groups'];
// no property 'payload' on CognitoIdToken
The console with the debugger.
$ user.signInUserSession.idToken.payload['cognito:groups']
> ['tutors']
// also works in console
$ user.getSignInUserSession().idToken.payload['cognito:groups']
> ['tutors']
The only typescript suggestion is to use user.getSignInUserSession()
method, but that doesn't work. Why is there confusion with the type definitions about what is there and what isn't?
英文:
I'm using the useAuthenticator
hook from AWS Amplify and I'm confused why certain properties do not exist at compile time. I get the error Property 'signInUserSession' does not exist on type 'AmplifyUser'
. However, when setting a debugger, I get to see that it does exist at runtime.
import { useAuthenticator } from "@aws-amplify/ui-react";
const { route, user } = useAuthenticator((context) => [context.route, context.user]);
const userGroup = user.signInUserSession.getIdToken.payload['cognito:groups'];
// no property 'signInUserSession'
const userGroup = user.getSignInUserSession()?.getIdToken.payload['cognito:groups'];
// no property 'payload' on CognitoIdToken
The console with the debugger.
$ user.signInUserSession.idToken.payload['cognito:groups']
> ['tutors']
// also works in console
$ user.getSignInUserSession().idToken.payload['cognito:groups']
> ['tutors']
The only typescript suggestion is to use user.getSignInUserSession()
method, but that doesn't work. Why is there confusion with the type definitions about what is there and what isn't?
答案1
得分: 1
以下是要翻译的内容:
The only typescript suggestion is to use user.getSignInUserSession() method, but that doesn't work.
唯一的TypeScript建议是使用user.getSignInUserSession()方法,但这并不起作用。
Theres a related Github issue open here and the author had also posted their solution:
...which worked for me when I ran into a similar issue.
这里有一个相关的Github问题链接在此,作者还发布了他们的解决方案:
......在我遇到类似问题时,这对我起到了作用。
Why is there confusion with the type definitions about what is there and what isn't?
关于类型定义有什么存在和不存在的混淆?
Didn't look too much into why, but if you scroll down on the linked issue there is an RFC published last month to make improvements on Amplify JS Typescript.
我没有深入研究原因,但如果你在链接的问题中向下滚动,上个月发布了一份关于改进Amplify JS TypeScript的RFC。
英文:
> The only typescript suggestion is to use user.getSignInUserSession() method, but that doesn't work.
Theres a related Github issue open here and the author had also posted their solution:
...which worked for me when I ran into a similar issue.
> Why is there confusion with the type definitions about what is there and what isn't?
Didn't look too much into why, but if you scroll down on the linked issue there is an RFC published last month to make improvements on Amplify JS Typescript.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论