如何在界面中使用 process.env.Namespace 作为 react 打字稿的键?

时间:2020-12-23 01:53:29

标签: reactjs typescript environment-variables

这是我从 auth0 获取用户时的用户界面。

export interface IUser {
  family_name: string;
  given_name: string;
  locale: string;
  name: string;
  nickname: string;
  picture: string;
  sub: string;
  updated_at: string;
  process.env.AUTH0_NAMESPACE + "/roles":string
}

因为我在 auth0 服务器上设置了命名空间,所以它被附加为使用用户对象作为字符串。由于我想对用户隐藏它,我认为这种方式会起作用,但我收到此错误:

 Property or signature expected.// this warning comes for process.

 'string' only refers to a type, but is being used as a value here.// this is for type "string".

我将此行转换为:

   `${process.env.AUTH0_NAMESPACE!}/roles`:string

我遇到了同样的错误。有没有办法使用 env 变量作为接口的键值,或者我只需要明确地写出键是什么。 我在这里使用这个接口:

export const isAuthorized = (user: IUser, role: string) => {
  return (
    user &&
    user[process.env.AUTH0_NAMESPACE! + "/roles"] &&
    user[process.env.AUTH0_NAMESPACE! + "/roles"].includes(role)
  );
};

当我将命名空间显式添加到接口时,我在 isAuthorized 中收到此错误:

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'IUser'.

在类型“IUser”上找不到带有类型“字符串”参数的索引签名

0 个答案:

没有答案