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?
CodePudding user response:
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
CodePudding user response:
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]
};