You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sorry for diverging from the issue template but IMO this isn't a bug but more of a question.
I'm using the useAuth hook to log in users in a React + TS app. I then want to process the user object further before returning it from my custom hook:
exportfunctionuseCustomAuth(){const{user: fronteggUser}=useAuth();functionmapUser(user: /* Which type to use here? */){// do something with the user}return{user: fronteggUser ? mapUser(fronteggUser) : null,};}
My problem is that accessing the type of the user returned from useAuth is hard. I can't easily get the type from the return type of useAuth directly due to useAuth being overloaded and TS not being able to pick the correct overload in this case AFAIU:
// In this case the type of userFromFrontegg is `unknown` ...functionmapUser(userFromFrontegg: Exclude<ReturnType<typeofuseAuth>,null|undefined>){// do something with the user}typeHookReturnType=ReturnType<typeofuseAuth>;// ... because this is `unknown`typeHookType=typeofuseAuth;// ... because this is `{ (): AuthState; <S>(stateMapper: AuthStateMapper<S>): S; }`
I can get the type from a variable but this seems suboptimal:
const{user: fronteggUser}=useAuth();functionmapUser(userFromFrontegg: Exclude<typeoffronteggUser,null|undefined>){// do something with the user}
My suggestion is to export the types AuthState, the return type of useAuth, and User from @frontegg/react to allow users to easily type the data returned by this hook. What do you think? Is there an easier way make this work maybe?
Thanks in advance for the help :)
The text was updated successfully, but these errors were encountered:
lx4r
changed the title
How to access the type of the user returned from useAuth?
How to easily access the type of the user returned from useAuth?
Mar 8, 2024
@lx4r Currently, you can access it via one of our internal libraries this way - import { User } from '@frontegg/redux-store';
We will expose it soon from the @frontegg/react package as well
Sorry for diverging from the issue template but IMO this isn't a bug but more of a question.
I'm using the
useAuth
hook to log in users in a React + TS app. I then want to process the user object further before returning it from my custom hook:My problem is that accessing the type of the user returned from
useAuth
is hard. I can't easily get the type from the return type ofuseAuth
directly due touseAuth
being overloaded and TS not being able to pick the correct overload in this case AFAIU:I can get the type from a variable but this seems suboptimal:
My suggestion is to export the types
AuthState
, the return type ofuseAuth
, andUser
from@frontegg/react
to allow users to easily type the data returned by this hook. What do you think? Is there an easier way make this work maybe?Thanks in advance for the help :)
The text was updated successfully, but these errors were encountered: