“Property ‘signInUserSession’ does not exist on type ‘AmplifyUser’ but exists at runtime.”

huangapple go评论70阅读模式
英文:

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:
“Property ‘signInUserSession’ does not exist on type ‘AmplifyUser’ but exists at runtime.”
...which worked for me when I ran into a similar issue.

这里有一个相关的Github问题链接在此,作者还发布了他们的解决方案:
“Property ‘signInUserSession’ does not exist on type ‘AmplifyUser’ but exists at runtime.”
......在我遇到类似问题时,这对我起到了作用。

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:
“Property ‘signInUserSession’ does not exist on type ‘AmplifyUser’ but exists at runtime.”
...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.

huangapple
  • 本文由 发表于 2023年2月27日 06:20:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575350.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定