如何正确访问嵌套在父对象中的对象,以将其用作嵌套在所述父对象的其他对象中的方法的参数类型

How to properly access an object nested inside parent object to use it as a type for parameter of a method nested in other object of a said parent

提问人:ar_IGT 提问时间:8/2/2023 最后编辑:ar_IGT 更新时间:8/3/2023 访问量:47

问:

我有这样的代码:

type GenericFunction = (...args: [Map<string, boolean | number | string>, number, string]) => void;

interface GenericObject { [key: string]: boolean | GenericFunction | number | string | GenericObject };

const API: GenericObject = {};

在后面的代码中,我将添加一些其他对象,用属性和值填充它们,并将它们嵌套在父对象中。API

所述嵌套对象之一,其proeprty值为JS方法:

API.json.mfa.AuthenticateRequest = (
  username: string,
  password: string,
  cardNumber: string,
  loginType: API.json.player.core.LoginType
): void => {
  const self = this;
  ...
};

参数的类型应为。loginTypeLoginType

嵌套类型是值的对象:LoginType

API.json.player.core.LoginType = {
  MANUAL_LOGIN: 'MANUAL_LOGIN',
  CARDED_LOGIN: 'CARDED_LOGIN',
  CARDLESS_CONNECT_LOGIN: 'CARDLESS_CONNECT_LOGIN',
};

太糟糕了,在方法的此参数中引用 会导致 IntelliJ IDEA 中出现错误: 。LoginTypeloginTypeTS2503: Cannot find namespace API

因此,其中的部分以红色下划线表示,作为导致错误的代码。APIloginType: API.json.player.core.LoginType

如何从参数内部正确引用此对象,并将其用作类型?LoginType

或者,也许我需要声明一个常量并使用值对其进行初始化:?const LoginType = API.json.player.core.LoginType;

然后以某种方式将其导出为类型?

试过这个: 并从引用到嵌套对象更改为仅常量名称:const LoginType = API.json.player.core.LoginType;

(
  username: string,
  password: string,
  cardNumber: string,
  loginType: LoginType,
  egmId: string
)

但 IntelliJ 现在显示错误。TS2749: LoginType refers to a value, but is being used as a type here.

我根据@jcalz已经做过的事情制作了一个 TS Playground

type GenericFunction = (...args: [Map<string, boolean | number | string>, number, string]) => void;

interface GenericObject { [key: string]: boolean | number | string | GenericFunction | GenericObject };

interface ApiFunction extends GenericFunction {
    apiVersion: string;
    messageType: string;
    messageId: number;
    response: string;
    setMessageId: Function;
    setResponse: Function;
    username: string;
    password: string;
    cardNumber: string;
    loginType: typeof LoginType;
    egmId: string;
};

const LoginType = {
  MANUAL_LOGIN: 'MANUAL_LOGIN',
  CARDED_LOGIN: 'CARDED_LOGIN',
  CARDLESS_CONNECT_LOGIN: 'CARDLESS_CONNECT_LOGIN',
} as GenericObject;

const API: GenericObject = {};

API.json = {} as GenericObject;
API.json.mfa = {} as GenericObject;
API.json.player = {} as GenericObject;
API.json.player.core = {} as GenericObject;

API.json.mfa.AuthenticateRequest = function (
    this: ApiFunction,
    username: string,
    password: string,
    cardNumber: string,
    loginType: API.json.player.core.LoginType,
    egmId: string
): void {
  const self = this;
  // since: 1.0
  this.apiVersion = '1.5-SNAPSHOT';
  // since: 1.0
  this.messageType = 'AuthenticateRequest';
  // since: 1.0
  this.setMessageId = (messageId: number): void => {
    self.messageId = messageId;
  };
  // since: 1.0
  this.setResponse = (response: string): void => {
    self.response = response;
  };
  // since: 1.5
  this.username = username;
  // since: 1.5
  this.password = password;
  // since: 1.5
  this.cardNumber = cardNumber;
  // since: 1.5
  this.loginType = loginType;
  // since: 1.5
  this.egmId = egmId;
} as unknown as GenericFunction;

API.json.player.core.LoginType = {
  MANUAL_LOGIN: 'MANUAL_LOGIN',
  CARDED_LOGIN: 'CARDED_LOGIN',
  CARDLESS_CONNECT_LOGIN: 'CARDLESS_CONNECT_LOGIN',
} as GenericObject;
TypeScript 对象 类型 转换 参考

评论

0赞 jcalz 8/2/2023
您可能可以编写 或 ,但是如果没有您的问题的最小可重现示例,这只是我们无法验证的猜测(不幸的是,现在我的 IDE 无法“注意”类型)。您能否进行编辑以使其他人可以重现您的问题?它可以是一个仅嵌套一两个属性的玩具示例。typeof API.json.player.core.LoginType(typeof API)["json"]["player"]["core"]["LoginType"]APIany
0赞 ar_IGT 8/3/2023
编辑过,也许现在你可以重现它了?
0赞 jcalz 8/3/2023
不,仍然存在不相关的错误。你能解决这些问题吗?
0赞 ar_IGT 8/3/2023
我做了一个已经在 TS Playground 上的修复
0赞 jcalz 8/4/2023
但现在你的类型只是因为你以这种方式创建了它。我可以像这样修复这些错误,但我不知道这是否是你要找的。如果您可以编辑以使示例更小(更少的同级属性和更浅的嵌套以及更少的不相关注释),也许我会更好地理解它?如果没有,也许其他人会提供帮助。祝你好运!GenericObject// since: 1.0

答: 暂无答案