简体   繁体   English

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

[英]Access interface property value in same interface

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 .请注意,可能有更多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 .我想要实现的是,当传递 name 和 methodName 时,intellisense 应该能够直接告诉我应该将哪种类型的对象作为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?这甚至可能吗?

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]
};

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 操场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM