在同一接口中访问接口属性值

发布于 2025-02-10 19:26:34 字数 1183 浏览 2 评论 0原文

我想知道是否可以在同一接口声明中访问接口Prop值,以动态设置类型。

我正在尝试做这样的事情:

export type MethodNames = "IsFallmanagerUpdateAllowed" | "UpdateStammFallmanager";

export interface IsFallmanagerUpdateAllowed {
  plcParameter : StammFallmanagerParameter
}

export interface UpdateStammFallmanager {
  plcParameter : StammFallmanagerParameter
}

export interface ServerTaskParam<T> extends system.ServerTaskParam<T> {
  name       : `${Class.NAME}`,
  methodName : MethodNames,
  paramObj   : // here depending on the passed methodname type should be IsFallmanagerUpdateAllowed or UpdateStammFallmanager
  // paramObj   : T -> this is what I use atm but I want to make it more dynamic
}

请注意,可能有更多MethodNames

我要实现的是,当传递名称和方法名称时,Intellisense应该能够直接告诉我应以paramobj的方式传递哪种类型的对象。

如果可能的话,应该是这样的:

export interface ServerTaskParam extends system.ServerTaskParam {
  name       : `${Class.NAME}`,
  methodName : MethodNames,
  paramObj   : [ methodName ] -> use methodName value to refer to one or the other interface in the same namespace (pseudo syntax)
}

我现在正在搜索网络一段时间,但找不到任何东西。这甚至可能吗?

I'm wondering if it's possible to access an interface prop value in the same interface declaration in order to set the types dynamically.

I'm trying to do something like this:

export type MethodNames = "IsFallmanagerUpdateAllowed" | "UpdateStammFallmanager";

export interface IsFallmanagerUpdateAllowed {
  plcParameter : StammFallmanagerParameter
}

export interface UpdateStammFallmanager {
  plcParameter : StammFallmanagerParameter
}

export interface ServerTaskParam<T> extends system.ServerTaskParam<T> {
  name       : `${Class.NAME}`,
  methodName : MethodNames,
  paramObj   : // here depending on the passed methodname type should be IsFallmanagerUpdateAllowed or UpdateStammFallmanager
  // paramObj   : T -> this is what I use atm but I want to make it more dynamic
}

Note that there could be more MethodNames.

What I want to achieve is that when passing name and methodName the intellisense should be able to tell me directly which type of object should be passed as paramObj.

It should be something like this if possible:

export interface ServerTaskParam extends system.ServerTaskParam {
  name       : `${Class.NAME}`,
  methodName : MethodNames,
  paramObj   : [ methodName ] -> use methodName value to refer to one or the other interface in the same namespace (pseudo syntax)
}

I'm searching the net for a while now but couldn't find anything. Is this even possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半葬歌 2025-02-17 19:26:34

I think what you need is just a union:

export type ServerTaskParam = ({
  name       : `${Class.NAME}`,
  methodName : "IsFallmanagerUpdateAllowed",
  paramObj   : IsFallmanagerUpdateAllowed
} | {
  name       : `${Class.NAME}`,
  methodName : "UpdateStammFallmanager",
  paramObj   : UpdateStammFallmanager
}) & system.ServerTaskParam

I think what you need is just a union:

export type ServerTaskParam = ({
  name       : `${Class.NAME}`,
  methodName : "IsFallmanagerUpdateAllowed",
  paramObj   : IsFallmanagerUpdateAllowed
} | {
  name       : `${Class.NAME}`,
  methodName : "UpdateStammFallmanager",
  paramObj   : UpdateStammFallmanager
}) & system.ServerTaskParam

Playground

压抑⊿情绪 2025-02-17 19:26:34

您可以创建一个包含所有可能接口的复合类型,因此您可以使用此键使用此“更高类型”:

export interface IsFallmanagerUpdateAllowed {
  plcParameter : StammFallmanagerParameter
}

export interface UpdateStammFallmanager {
  plcParameter : StammFallmanagerParameter
}

interface Methods {
  IsFallmanagerUpdateAllowed: IsFallmanagerUpdateAllowed;
  UpdateStammFallmanager: UpdateStammFallmanager;
}

export interface ServerTaskParam<K extends keyof Methods> extends system.ServerTaskParam<T> {
  name       : `${Class.NAME}`,
  methodName : K,
  paramObj   : Methods[K]
};

You could create a composite type holding every possible interface so you can use this "higher type" with its keys like so:

export interface IsFallmanagerUpdateAllowed {
  plcParameter : StammFallmanagerParameter
}

export interface UpdateStammFallmanager {
  plcParameter : StammFallmanagerParameter
}

interface Methods {
  IsFallmanagerUpdateAllowed: IsFallmanagerUpdateAllowed;
  UpdateStammFallmanager: UpdateStammFallmanager;
}

export interface ServerTaskParam<K extends keyof Methods> extends system.ServerTaskParam<T> {
  name       : `${Class.NAME}`,
  methodName : K,
  paramObj   : Methods[K]
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文