提问人:ar_IGT 提问时间:8/2/2023 最后编辑:ar_IGT 更新时间:8/3/2023 访问量:47
如何正确访问嵌套在父对象中的对象,以将其用作嵌套在所述父对象的其他对象中的方法的参数类型
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
问:
我有这样的代码:
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;
...
};
参数的类型应为。loginType
LoginType
嵌套类型是值的对象:LoginType
API.json.player.core.LoginType = {
MANUAL_LOGIN: 'MANUAL_LOGIN',
CARDED_LOGIN: 'CARDED_LOGIN',
CARDLESS_CONNECT_LOGIN: 'CARDLESS_CONNECT_LOGIN',
};
太糟糕了,在方法的此参数中引用 会导致 IntelliJ IDEA 中出现错误: 。LoginType
loginType
TS2503: Cannot find namespace API
因此,其中的部分以红色下划线表示,作为导致错误的代码。API
loginType: 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;
答: 暂无答案
评论
typeof API.json.player.core.LoginType
(typeof API)["json"]["player"]["core"]["LoginType"]
API
any
GenericObject
// since: 1.0