What I am trying to achieve is primarily to pass the login user's email address to dict
as a key, get its corresponding token value, and use it to make an API call as a query parameter. The user's email address comes from returned user data when a user logs in, and I just need his/her email address from it. That's where the userInfo
interface is defined. In the following code snippet, the compiler throws an error by saying "Property 'email' is missing in type '{}' but required in type 'userInfo'." I fixed this issue by adding '?' at the end of the email
variable in the interface. Then it gives me another error: "Property 'email' of type 'string | undefined' is not assignable to string index type 'string'." I understand this issue because email
is the only property of the object and it should not be undefined. Due to those errors above, I have been totally stuck at this point. If you could give me a suggestion to resolve this issue, that would be greatly appreciated.
//tokenDict.tsx
export const tokenDict: { [email: string]: string } = {
"[email protected]": "sample_token1",
"[email protected]": "sample_token2",
};
//HomeScreen.tsx
interface userInfo {
email: string; //email vs email?
[key: string]: string;
}
const HomeScreen: React.FC = () => {
const user: userInfo = useGlobalState("user"); //user object returned from login process is stored in user state in Redux.
const userEmail: string = user.email;
const token = tokenDict[userEmail];